Vitalii Isaenko
Vitalii Isaenko

Reputation: 989

Inserting many records using sql trigger

I have two tables with 1 to many relationship. Here are they:

enter image description here

When inserting into ActivityAttribute I would like to use generated idAA for inserting many records to PersonActivityAttribute - one for each existing value of PersonActivityAttribute.idPA. It goes like this: when I insert a new record to ActivityAttribute with idAA = 5 I want the following result

enter image description here

The question is - how can I achive it using trigger? Is it possible?

Upvotes: 1

Views: 63

Answers (1)

Viki888
Viki888

Reputation: 2774

You can make use of the below query

CREATE TRIGGER trgAfterInsert on ActivityAttribute
FOR INSERT
AS DECLARE @idAA INT;
SELECT @idAA=i.idAA FROM inserted i;
INSERT INTO PersonActivityAttribute(idPA,idAA,value)
SELECT DISTINCT idPA, @idAA, NULL value
FROM PersonActivityAttribute;

Hope this should help you out.

Upvotes: 1

Related Questions