首页 > 解决方案 > 为每个外键插入一行

问题描述

我有一个表,它的名称是“评论”,它有“客户”表的外键,如下所示:

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 ','

标签: sqlsql-servertsql

解决方案


VALUES不需要只使用以下SELECT语句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语句一​​起使用。


推荐阅读