Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert more than one row

Author  Topic 

duf
Starting Member

39 Posts

Posted - 2012-07-07 : 08:43:10
Is it possible to insert rows as a answer of other query?

INSERT INTO TABLE SET ID=0, ID=1 etc.

INSERT INTO TABLE SET ID=(SELECT ID FROM TABEL1) - In this case answer form SELECT has more than one result what to do?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-07 : 08:48:40
Yes. The syntax is:
INSERT INTO YourTable1 (ID) SELECT ID FROM YourTable2;
The column names do not have to be the same. In the above first (ID) is the column name in the destination table and the second ID is the column name in the source table.

If you have other columns in the destination table, they have to be either nullable, or you have to provide values for those columns. So the general syntax would be
INSERT INTO YourTable1 (Col1,Col2,Col3) SELECT ColA, ColB, ColC FROM YourTable2;
http://msdn.microsoft.com/en-us/library/ms174335.aspx
Go to Top of Page
   

- Advertisement -