0

I'm working on a trigger in Microsoft SQL Server 2012

Here is what I have so far, following what I know to be proper syntax:

CREATE TRIGGER restore_trigger
ON dbo.Lead 
AFTER DELETE OF id, firsname, lastname, postalcode, address,phone, email, title, managementlevel, lastcalldate, lastcallresult, companyid
REFERENCING OLD ROW as OldRow 
FOR EACH ROW
     INSERT INTO dbo.Lead 
     VALUES (OldRow.id, OldRow.firstname, OldRow.lastname, OldRow.postalcode, OldRow.address, OldRow.phone, OldRow.email,
OldRow.title, OldRow.managmentlevel, OldRow.lastcalldate, OldRow.lastcallresult, OldRow.companyid);

Microsoft SQL Server is throwing an error with the 'of' and all of the values from OldRow I want to insert in to dbo.Lead.

5
  • You can only use the OF keyword with an INSTEAD. msdn.microsoft.com/en-us/library/ms189799.aspx Commented Dec 3, 2016 at 20:55
  • 2
    This is NOT correct syntax for a trigger in SQL Server - plain and simple. SQL Server does NOT have a FOR EACH ROW clause, T-SQL does not support AFTER DELETE of ....... - and T-SQL doesn't support the OLD or NEW keywords - you need to read the official MSDN documentation on the exact trigger syntax for T-SQL !! Commented Dec 3, 2016 at 20:56
  • @marc_s This is the syntax my professor taught me. If I wanted to run a trigger like this what would I do? Can I get an example please? I was looking at the documentation earlier and I can't make much sense of it. Commented Dec 3, 2016 at 21:05
  • 2
    As I said: this is NOT T-SQL / SQL Server syntax! It looks more like Oracle to me. If your professor taught you this as SQL Server syntax, he is PLAIN WRONG. Read the documentation - I provided the link to it ! Commented Dec 3, 2016 at 21:06
  • 1
    Please...when working with languages, check your sources. It is not difficult to check up MSDN's documentation on SQL Server, and the detail is superb. If you care about learning programs...learn to read what the pros read! Commented Dec 4, 2016 at 8:54

1 Answer 1

1

As I said - your current code is full of syntax and logic errors when it comes to T-SQL triggers.

If you want to "save" the old values (that are being deleted), you can use this code:

CREATE TRIGGER restore_trigger
ON dbo.Lead 
AFTER DELETE  
AS
    INSERT INTO dbo.Lead (id, firstname, lastname, postalcode, address, phone, email, title, managmentlevel, lastcalldate, lastcallresult, companyid)
        SELECT 
            id, firstname, lastname, postalcode, address, phone, email, title, managmentlevel, lastcalldate, lastcallresult,  companyid
        FROM
            Deleted;

Notice:

  • I would recommend to always specify the columns you're inserting into - no exception.
  • You only need to have a single SELECT statement that takes all the rows from the Deleted pseudo table and inserts those values into the target table - this will handle multi-row deletes, too (if a statement of yours deletes 20 rows)

But I'm not clear why you'd want to INSERT the deleted rows back into the table from which they're being deleted...... what's the purpose of that?? Typically, you'd much rather be storing those values into an Audit table or something.....

And if you want to basically undo the effects of a DELETE - why not just preventing the DELETE in the first place, by removing the permission to delete from that table from those users who are not allowed to delete?.... Seems a lot more efficient to prevent an unwanted operation, rather than undoing it in a trigger....

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I realize that this trigger is kinda trivial and has no real practical application. I was required to write any kind of trigger I wanted for the database. This is the first thing I could come up with that did not involve having to make a new table to hold some value/statistics that were calculated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.