-1

I have realized that the syntax for triggers is slightly different for different software. What would be the syntax for the following piece of code in SQL Server 2012?

Create trigger before_playercatalogue_update
before update 
on player_catalogue
For each row
Begin
    Insert into player_audit
    set action = 'update',
        playerid = old.playerid
        fname = old.fname,
        datachange = (Now);
End
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kevin G
  • 119
  • 1
  • 8
  • Is there an issue with the code you provided? Are you getting an error message or assuming there are syntax differences between sql server 2012 and another version? –  Apr 15 '16 at 02:17
  • 1
    Have a look at this post http://stackoverflow.com/questions/642822/how-can-i-do-a-before-updated-trigger-with-sql-server – Thomas Apr 15 '16 at 02:18

1 Answers1

0

The syntax is quite different. It would look like:

Create trigger after_playercatalogue_update
on player_catalogue after update
as
Begin
    Insert into player_audit(action, playerid, fname, datachange)
        select 'update', playerid, fname, getdate()
        from inserted;
End;

Note some of the changes:

  • This is an after trigger. SQL Server doesn't have "before" triggers.
  • The set clause is not supported for insertin SQL Server (or other databases).
  • SQL Server does not have "new" and "old". It uses inserted, a view on the records that have changed.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 1
    SQL also doesn't have `for each row` but in this case it doesn't matter, it's functionally the same. – Nick.Mc Apr 15 '16 at 02:27