Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to Write Trigger in Sql Server 2005 Expand / Collapse
Author
Message
Posted Wednesday, March 14, 2007 2:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 27, 2011 9:14 AM
Points: 20, Visits: 18

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

Post #351362
Posted Wednesday, March 14, 2007 5:59 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 24, 2007 2:31 AM
Points: 111, 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

 

 

Post #351388
Posted Wednesday, March 14, 2007 6:20 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, October 23, 2012 4:41 PM
Points: 75, 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

Post #351397
Posted Thursday, March 15, 2007 2:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 27, 2011 9:14 AM
Points: 20, Visits: 18

Hello All,

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

With Best Regards

   Abhay

Post #351713
Posted Wednesday, November 26, 2008 9:16 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 17, 2009 10:24 AM
Points: 4, 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'
Post #609225
Posted Thursday, June 23, 2011 2:35 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 6, 2014 1:59 PM
Points: 1, 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/
Post #1130833
Posted Monday, August 22, 2011 8:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 22, 2011 8:43 AM
Points: 1, 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
Post #1163344
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse