Skip to main content

Triggers in Sql Server!

What is a Trigger
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You can’t explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.
Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.

Basically, triggers are classified into two main types:-

(i) After Triggers (For Triggers)
(ii) Instead Of Triggers


(i) After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views.
AFTER TRIGGERS can be classified further into three types as:

(a) AFTER INSERT Trigger.
(b) AFTER UPDATE Trigger.
(c) AFTER DELETE Trigger.


(ii) Instead Of Triggers
These can be used as an interceptor for anything that anyonr tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger)
INSTEAD OF TRIGGERS can be classified further into three types as:-

(a) INSTEAD OF INSERT Trigger.
(b) INSTEAD OF UPDATE Trigger.
(c) INSTEAD OF DELETE Trigger.


Examples



CREATE TABLE [dbo].[tblEmp](
[EmpId] [int] NULL,
[Ename] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Age] [int] NULL
) ON [PRIMARY]



CREATE TABLE [dbo].[tblEmp_dup](
[EmpId] [int] IDENTITY(1,1) NOT NULL,
[Ename] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Age] [int] NULL,
[Action] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_tblEmp_dup] PRIMARY KEY CLUSTERED
(
[EmpId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

After Triggers:

1)For Insert
Create trigger [dbo].[trginsertemp] on [dbo].[tblEmp] After insert
As
Declare @EmpId int;
Declare @Empname varchar(50);
Declare @Age int;
Select @EmpId=EmpId from inserted
Select @Empname=Ename from inserted
Select @Age=Age from inserted
insert into tblEmp_dup values(@Empname,@Age,'insert')
2)For Update
Create trigger [dbo].[trgupdateEmp] on [dbo].[tblEmp] for update
As
Declare @Enameold as varchar(50);
Declare @Ageold as int;
Declare @Enamenew as varchar(50);
Declare @Agenew as int;
Select @Enameold=Ename,@Ageold=Age from deleted
Select @Enamenew=Ename,@Agenew=Age from inserted
insert into tblEmp_dup values(@Enameold,@Ageold,'before update')
insert into tblEmp_dup values(@Enamenew,@Agenew,'After update')

3)For Delete
Create trigger [dbo].[trgdeleteEmp] on [dbo].[tblEmp] for delete
As
Declare @Ename as varchar(50);
Declare @Age as int;
Select @Ename=Ename,@Age=Age from deleted
insert into tblEmp_dup values(@Ename,@Age,'Deleted Record’)
)



Instead of Triggers for Delete:

ALTER trigger [dbo].[trgdeleteEmp] on [dbo].[tblEmp] INSTEAD OF DELETE
As
Declare @Ename as varchar(50);
Declare @Age as int;

if(@Age>25)
Begin
RAISERROR('Cannot delete where age > 25',16,1)
Rollback;
End
else
Begin
insert into tblEmp_dup values(@Ename,@Age,'Deleted Record')
Commit;
End

Comments

Popular posts from this blog

URL Rewritting

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/ http://msdn.microsoft.com/en-us/library/ms972974.aspx URL Rewriting in ASP.NET Summary: Examines how to perform dynamic URL rewriting with Microsoft ASP.NET. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting, and examines real-world scenarios of URL rewriting. (31 printed pages) Download the source code for this article. Contents Introduction Common Uses of URL Rewriting What Happens When a Request Reaches IIS Implementing URL Rewriting Building a URL Rewriting Engine Performing Simple URL Rewriting with the URL Rewriting Engine Creating Truly "Hackable" URLs Conclusion Related Books Introduction Take a moment to look at some of the URLs on your website. Do you find URLs like http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summ...

SEND A PDF FILE AS AN ATTACHEMENT OF MAIL!

System.Net.Mail.MailMessage m1 = new System.Net.Mail.MailMessage(); m1.From = "manpreet@gmail.com" m1.Subject = "Test mail "; m1.Body = str.ToString(); m1.IsBodyHtml = true; m1.To.Add("jasdeep@gmail.com"); m1.CC.Add("jasdeep123@gmail.com"); m1.Attachments.Add(new Attachment(strserverpath + @"\pdf\" + PdfFileName)); smtp.Send(m1);

Sql Server Tips

IF WE WANT TO SELECT  TOP N ROWS  FROM A TABLE ,  WE USE THE FETCH NEXT  SELECT    * FROM  table1   ---//First m rows ignore first n rows ORDER BY id OFFSET n ROWS FETCH NEXT m ROWS ONLY if n=0, m=10 then first  10 rows if n=10, m=10 then first  10 rows start from 11th row Row_number()   SELECT  ROW_NUMBER() OVER(order by db.id) Insertion Simultaneouly in temp table with output clause DECLARE @table1 table (   Id int,   name nvarchar(50) ); DECLARE @table2 table (  Id int,  name nvarchar(50) ); INSERT INTO @table2 OUTPUT INSERTED.*   INTO @table1   select top 10 id, name  from FinalTable SELECT * FROM @table2; SELECT * FROM @table1;