Hi everybody,
I just wrote my first two triggers and from the minimal amount of testing I
have done, they work! However, I was hoping I could get some feedback from
those of you more experienced in writing triggers.
Here is the first one:
CREATE TRIGGER DecreInters ON Interaction
FOR DELETE
AS
declare @stu INT
declare @num INT
select @stu = Student_FK from deleted
select @num = (select Inters from Student where Student_Key = @stu)
UPDATE Student
SET Inters = @num - 1
FROM Student
WHERE Student.Student_Key = @stu
Here is the second one:
CREATE TRIGGER IncreInters
ON Interaction
AFTER INSERT
AS
declare @stu INT
declare @num INT
declare @last_rec INT
select @last_rec = @@IDENTITY
select @stu = (select Student_FK from Interaction where Interaction_ID =
@last_rec)
select @num = (select Inters from Student where Student_Key = @stu)
UPDATE Student
SET Inters = @num + 1
FROM Student
WHERE Student.Student_Key = @stu
Are there any shortcuts I could use or things I could do to make these
triggers more efficient. Please give me some feedback and let me know of
any problems that might possibly be caused due to my doing this improperly.
Thanks ahead,
Corey