SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Write Trigger in Sql Server 2005


How to Write Trigger in Sql Server 2005

Author
Message
Abhay Jadhav
Abhay Jadhav
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 21

Hello all,

I want to know that, if i want to write trigger in sql server 2005 from where should i do it. or if i want to view trigger which has been already there for table.

plz help me out.

thx in advance

Abhay


Jayanthi-392233
Jayanthi-392233
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 1

Hi,

you have to write it in query analyzer for a particular table. The procedure is same as in sql server 2000 or from the database expand tables then expand the particular table which u want write the trigger then right click the triggers and u get the "New Trigger" option.

regards,

Jayanthi


Jeba_Antony
Jeba_Antony
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 33

Hi,

Trigger is mainly used for the situation when the db automeically trigger some events.

DML Triggers are,

Insert trigger,update trigger,delete trigger

Assume u write the delete trigger for your table.

whenever a row is deleting that table the trigger will raise and the corresponding codes will be executed.

for ex: In your delete trigger u delete the subcategories and products.

assume trigger table consists of categories.

whenever the category is deleting your delete trigger will raise and autometically corresponding subcategories and products will be deleted.

Like that works all trigger events.

With Regards

Antony


Abhay Jadhav
Abhay Jadhav
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 21

Hello All,

Thx for your comments. Thx Jayanthi for your valuable guidance. It solved my problem...

With Best Regards

Abhay


Veena Pande
Veena Pande
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 34
You can get a list of all the triggers in the database by following command in Query Analyzer.

select name from sysobjects where type = 'tr'

you can view the code of a trigger with following command

sp_helptext 'triggername'
Anthony2
Anthony2
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 20
There's no UI way to write a trigger that I know of, I've always used Query Analyzer to write mine.

Here's a quick example of the syntax of a trigger, of course the tables have to exist for this to work:
create trigger dbo.audit_SomeTable on dbo.SomeOriginTable for update
as
if update(desired_date) --if the desired_date field is changing do the insert
begin
insert into dbo.SomeTable_audit (rowid,FromDATE,ToDATE)
select i.rowid,d.desired_date,i.desired_date
from inserted i
join deleted d on
i.rowid = d.rowid
end
go



I took the code above from this fellow's blog:
http://anthonystechblog.wordpress.com/2011/06/14/how-to-write-a-trigger-in-sql-server/
awadhendratiwari
awadhendratiwari
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 0
check Articles on Trigger:
http://www.mindstick.com/Articles/d9d0f625-6146-415b-b286-3319e3ec336f/?Trigger%20in%20SQL%20server
http://www.mindstick.com/Articles/8a50890e-16b5-4fc6-8970-54ae61a898f9/?Triggers%20in%20SQL%20Server
http://www.mindstick.com/Articles/72769122-ae03-49c6-9017-7aa239e7b49b/?Implementing%20Triggers%20in%20Sql%20Server
http://www.mindstick.com/Articles/f7074849-0e9e-463b-a12d-f3d7a35b2785/?Trigger%20in%20SQL%20Server
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search