SQL 服务器插入到

SQL Server insert into

我正在尝试插入到 table1(column1, column2, column3) 中,其中 column1 有多个值。

例如:

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

错误:

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.

只需使用 Insert into..Select 语法。试试这个

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

注意:根据问题中提到的错误,上述插入查询将插入多行。如果您不希望发生这种情况,请通过添加 Where 子句(过滤器)或 TOP

来限制行