Orhun
Orhun

Reputation: 1252

Insert a row for each foreign key

I have a table which its name is 'comment' and it has foreign key for 'customer' table as below:

id
customer_id(FK)
content
...

I have a lot of foreign keys(customer_id) and I want to insert a comment row for each foreign key to comment table with same values.

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);

It gives error like this:

Incorrect syntax near ','

Upvotes: 0

Views: 69

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

VALUES is not needed just use SELECT statement with 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);

You can use constant expression with SELECT statement.

Upvotes: 3

Related Questions