It is possible to do this in SQL Server Triggers?

  • Hi,

    It is possible to do something like this in SQL Server Triggers?

    If INSERT, do this;

    If UPDATE, do that;

    It is possible?

    Best Regards,

  • Create one trigger FOR INSERT and put the insert logic in that, create a second trigger FOR UPDATE and put the update logic in there.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Yes, basically you will have rows in inserted and deleted pseudotables for UPDATE, in inserted for INSERT and in deleted only for DELETE.

    So you'd have to check something like

    if(exists (select 1 from inserted) and not exists(select 1 from deleted)) -- this is INSERT

    if(exists (select 1 from inserted) and exists(select 1 from deleted)) -- this is UPDATE

    ...

    Regards

    Piotr

    ...and your only reply is slàinte mhath

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply