Friday 14 November 2014

Create Trigger in SQL server

A lazy developer should know about Trigger ;) , Now we are going to learn.. :P Not learn just know How to create trigger in SQL server.

here is the structure ,    
CREATE TRIGGER trigname ON mytable
FOR INSERT/UPDATE/DELETE
AS
  BEGIN
  /* Trigger Body*/
  END 



Here    trigname is my trigger name
            mytable is my table name
            INSERT/UPDATE/DELETE you can use either single from these.
if you want to trigger your code in both  INSERT and UPDATE , you can use  them like this  INSERT,UPDATE [Separate by comma (,)].

Please not that , Instead of  FOR you can use   AFTER | INSTEAD OF  these guys as per your needs.

And..................

You can access the inserted guys in trigger if you want  by using this

DECLARE @MyVariable BIGINT

SELECT @MyVariable = I.myfield
FROM   inserted I; 


 :) eee

No comments:

Post a Comment