Sql-server – How to we select data from another table during an insert

sql server

I'm trying to insert data into a table using a few columns that are hard-coded or are static values and then the rest of the datat selected from another table. I've tried the following query:

insert into wtdocversion 
values (( select id from wtdocmaster),'col2','col3');

But this generates an error:

Msg 512, Level 16, State 1, Line 1

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Best Answer

You want to drop the values part of the query and do an INSERT\SELECT.

This is really basic SQL and you can read more about it in BOL.

INSERT INTO wtdocversion (col1, col2, col3)
SELECT
  id,
  'col2',
  'col3'
FROM wtdocmaster;