Search code examples
sqldatabaseinsert-into

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

Solution

  • 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.