redninam February 2016

SQL Server insert into

I am trying to insert into table1(colunm1, column2, column3) where column1 has multiple values.

ex:

INSERT INTO table1 (col1, col2, col3)
VALUES (1419002, 1003,
         (select TaskTypeCode 
          from TaskTypeCodes 
          where TaskTypeID in (898,788,878,874)) )

Error:

Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression. The statement has been terminated.

Answers


MotoGP February 2016

Just use Insert into..Select syntax. Try this

INSERT INTO table1 (col1, col2, col3)
SELECT 1419002, 1003, TaskTypeCode 
FROM TaskTypeCodes 
WHERE TaskTypeID in (898,788,878,874)

Note: Based on the error mentioned in question the above Insert query will insert more than one row. If you don't want that to happen then restrict the rows by adding Where clause(filter's) or TOP

Post Status

Asked in February 2016
Viewed 2,129 times
Voted 11
Answered 1 times

Search




Leave an answer