We help IT Professionals succeed at work.
Get Started

Update trigger for bulk update

dodgybouy
dodgybouy asked
on
1,360 Views
Last Modified: 2010-05-19
have a trigger that writes to an audit table when this table is updated.  It seems to fail when I run a bulk update.  What's the best way to get this to work?  Is it possible to do this without using a cursor

ALTER   trigger tr_u_Student

ON Student
after UPDATE

AS

BEGIN
     DECLARE @Student_ID NVarChar(15), @StudentName NVarChar(80), @CreatedBy NVarChar(30)

     SET @Student_ID = (SELECT Student_ID FROM DELETED)
     SET @StudentName = (SELECT [Forename 1] + ' ' + [Surname] FROM INSERTED
          WHERE Student_ID IN (SELECT Student_ID FROM DELETED)
     )
     SET @CreatedBy = 'System'


     INSERT INTO Audit
     (
          Action,
          AffectedTable,
          Description,
          ActionDate,
          UserID
     )

     SELECT 'Student record updated' as Action,
            'Student' as AffectedTable,
            'Record updated for StudentID: ' + @Student_ID + ', ' + @StudentName,
            GetDate(),
            @CreatedBy

     FROM DELETED
END
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE