How to insert the contents of a subquery into MS SQL Server?

I want to do something like

insert into my table (select * from anothertable where id < 5)

What is the correct MSSQL syntax?

Thank!

+3
source share
4 answers

Is this what you are looking for?

INSERT INTO MyTable
SELECT * FROM AnotherTable
WHERE AnotherTable.ID < 5
+5
source

This syntax looks correct, but you must match the fields, otherwise it will not work.

You can specify fields, for example:

INSERT INTO myTable(COL1, COL2, COL3) 
SELECT COL1, COL2, COL3 FROM anotherTable where anotherTable.id < 5
+3
source

Insert Into MyTable ( Col1, Col2, Col3 ) Select Col1, Col2, Col3 From AnotherTable Where ID < 5

0
source

You can also do

select *
into MyTable
from AnotherTable
where ID < 5

which will create MyTable with the necessary columns, as well as fill in the data.

0
source

Source: https://habr.com/ru/post/1698536/


All Articles