jordan
jordan

Reputation: 3546

SQL - how to retrieve mutlple rows from a table, and then take that info and dump it into a new row on a different table

I want to take rows from one table, based on a typeID number, and insert a new row into a different table using a mix of data from the first table query and some static variables .

Is there a easy way to do this?

Code edit: (I cannot get this to work - get a MIssing Expression error )

Insert into tableOne
(pk_col, Custom_int_col, Data_from_other_col)
Select  default,111,security_resource_id
From security_resource sr
Where sr.company_id = 1

Upvotes: 0

Views: 64

Answers (3)

Mahmoud ElMansy
Mahmoud ElMansy

Reputation: 71

yes sure

    INSERT INTO yourTable
    (column1,column2)
    SELECT '' ,column FROM SecondTa

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269853

If the second table doesn't exist, you can create it using either create table as or select into, depending on the database you are using.

For instance:

select col1, 123 as value
into NewTable
from t
where flag = 0

In some databases, the syntax would be:

create table as
    select col1, 123 as value
    from t
    where flag = 0

Tony already answered the question for the situation where the second table already exists.

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Something like

Insert SomeTable(SomeCol1, SomeCol2,SomeCol29)
Select 'SomeText', SomeCol3,963.45 From SomeOtherTable Where SomeKey = 876

There's another flavour called select into as well. Can't get exact with the syntax, because you never mentioned which DBMS you are using.

Upvotes: 2

Related Questions