为每个外键插入一行

时间:2018-10-09 10:07:21

标签: sql sql-server tsql

我有一个表,其名称为“ comment”,并且具有“ customer”表的外键,如下所示:

id
customer_id(FK)
content
...

我有很多外键(customer_id),并且我想为每个外键插入一个注释行以注释具有相同值的表。

insert into comment (customer_id, content, modified_date, modified_by, created_date, created_by, is_deleted, [application])
values(select id from customer 
       where id in(66417,65407,82589,71318,82915... many FKs), 'this is the new string value', NULL, 0, GETDATE(), 110, 0, 0);

它给出了这样的错误:

Incorrect syntax near ','

1 个答案:

答案 0 :(得分:3)

只需将VALUESSELECT一起使用,就不需要

INSERT INTO ..

insert into comment (customer_id, content, modified_date, modified_by, created_date, created_by, is_deleted, [application])
    select id, 'this is the new string value', NULL, 0, GETDATE(), 110, 0, 0
    from customer 
    where id in (66417,65407,82589,71318,82915... many FKs);

您可以在SELECT语句中使用常量表达式。