Trigger is a special kind of stored procedure that executes in response to certain actions on the table like insertion, updation and deletion. There are 2 types of triggers
- After Triggers (for triggers)
- Instead Of Triggers.
After Triggers
After trigger are further classified into 3 types,
- After Insert: Fired after insert operation is performed on the table.
- After Update: Fired after update operation is performed on the table.
- After Delete: Fired when a record is deleted from a table.
Now we will see the triggers in action from a small example. For this we will first create 2 tables.
- CREATE TABLE [dbo].[Blogs](
- [blog_id] [int] IDENTITY(1,1) NOT NULL,
- [blog_title] [varchar](max) NOT NULL,
- [blog_date] [date] NOT NULL,
- [blog_description] [varchar](max) NOT NULL,
- [blog_tags] [varchar](max) NOT NULL,
- [status] [bit] NULL,
- [blog_url] [varchar](max) NULL,
- CONSTRAINT [PK_Blogs] PRIMARY KEY )
- GO
- CREATE TABLE [dbo].[Blog_tag](
- [Blog_id] [int] NOT NULL,
- [Tag_id] [int] NOT NULL
- ) ON [PRIMARY]
- GO
- CREATE TABLE [dbo].[Tags](
- [Tags_id] [int] IDENTITY(1,1) NOT NULL,
- [Tag_name] [varchar](50) NOT NULL,
- CONSTRAINT [PK_Tags] PRIMARY KEY
- )
- GO
- INSERT INTO [dbo].[Tags]
- ([Tag_name])
- VALUES
- ('Sql Server')
- GO
- Create trigger [dbo].[trgAfterInsert] on [dbo].[Blogs]
- After Insert
- As
- declare @blog_id int;
- declare @tag_id int;
- declare @tagname varchar(50);
- select @blog_id=i.blog_id from inserted i;
- select @tagname=i.blog_tags from inserted i;
- select @tag_id=Tags_id from Tags where Tag_name=@tagname;
- Insert into Blog_tag(Blog_id,Tag_id) values(@blog_id,@tag_id);
- INSERT INTO [dbo].[Blogs]
- ([blog_title]
- ,[blog_date]
- ,[blog_description]
- ,[blog_tags]
- ,[status]
- ,[blog_url])
- VALUES
- ('Test'
- ,'2016-02-06'
- ,'Test Description'
- ,'Sql Server'
- ,0
- ,'test url')
- GO
- Create trigger [dbo].[trgAfterUpdate] on [dbo].[Blogs]
- After Update
- As
- declare @blog_id int;
- declare @tagname varchar(50);
- declare @tag_id int;
- select @blog_id=i.blog_id from inserted i;
- select @tagname=i.blog_tags from inserted i;
- select @tag_id=Tags_id from Tags where Tag_name=@tagname;
- print @tagname;
- Update Blog_tag set Tag_id=@tag_id where Blog_id=@blog_id;
- Create trigger [dbo].[trgAfterDelete] on [dbo].[Blogs]
- After Delete
- As
- declare @blog_id int;
- select @blog_id=i.blog_id from deleted i;
- delete from Blog_tag where Blog_id=@blog_id;
Instead of Trigger
These are used when we want to check certain conditions before performing insert, update or delete on a table. These are further classified into 3 types:
- Instead of Insert: These will fire when we will insert a record in a table and will perform the specified query instead of insert.
- Instead of Update: These will fire when we will insert a record in a table and will perform the specified query instead of update.
- Instead of delete: These will fire when we will insert a record in a table and will perform the specified query instead of delete. Now i will show how to create instead of delete trigger.
- Create trigger[dbo].[trgInsteadDelete] on[dbo].[Blogs]
- instead of Delete
- As
- declare @blog_id int;
- declare @tag_id int;
- select @blog_id = i.blog_id from deleted i;
- select @tag_id = Tag_id from Blog_tag where Blog_id = @blog_id
- begin
- if (@tag_id is null) begin
- rollback;
- end
- else begin
- delete from Blog_tag where Blog_id = @blog_id;
- end
- end

Santhakumar MunuswamyPosted Feb 13, 2016, 12:21 AM
Thanks for nice share