Tuesday, August 24, 2010

MS-SQL - Enable and Disable Trigger in MS-SQL

Step 1
Create a database and table,it is a sample example


   1:  CREATE DATABASE Test
   2:   
   3:  USE Test
   4:   
   5:  CREATE TABLE Student
   6:  (
   7:      StudentID int identity(1,1) primary key,
   8:      FirstName varchar(100),
   9:      LastName varchar(100)
  10:  )

Step 2
Create a trigger on Student Table

   1:  CREATE TRIGGER Tri_Student
   2:  ON Student
   3:  FOR INSERT
   4:  AS
   5:      PRINT 'Trigger Fired Successfully'
   6:  GO

Step 3
Insert a data for cheking trigger running or not

   1:  INSERT INTO Student 
   2:  (FirstName,LastName)
   3:  VALUES
   4:  ('kishor','naik') 
then we will get a trigger message.

Step 4
Disable the trigger


   1:  DISABLE TRIGGER Tri_Student
   2:  ON Student

Insert one Record
   1:  INSERT INTO Student 
   2:  (FirstName,LastName)
   3:  VALUES
   4:  ('bhavesh','pisat') 
So after inserting data.we will not get any trigger message.

Step 5
Enable the Trigger

   1:  ENABLE Trigger Tri_Student
   2:  ON Student

Insert one Record

   1:  INSERT INTO Student 
   2:  (FirstName,LastName)
   3:  VALUES
   4:  ('kedar','despande')
So after inserting a data. we will get a trigger message

No comments:

Post a Comment