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

Triggers Examples Expand / Collapse
Author
Message
Posted Tuesday, October 14, 2008 2:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 07, 2009 11:39 PM
Points: 11, Visits: 22
Hi, Can any one send me the simple examples of using Instead of Trigger and After Trigger with Northwind database tables.

I want to create the triggers and execute them.
Post #585290
Posted Tuesday, October 14, 2008 3:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 38,079, Visits: 30,374
What are you trying to do with triggers?


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #585302
Posted Tuesday, October 14, 2008 4:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 07, 2009 11:39 PM
Points: 11, Visits: 22
The trigger should insert records into tables.
I need only general examples.
Post #585332
Posted Tuesday, October 14, 2008 4:41 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, May 16, 2013 4:02 AM
Points: 3,131, Visits: 1,056
Check the following link

http://www.sqlservercentral.com/articles/Triggers/64214/



Post #585336
Posted Tuesday, October 14, 2008 5:35 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 38,079, Visits: 30,374
tochaturvedi (10/14/2008)
The trigger should insert records into tables.
I need only general examples.


CREATE TRIGGER trg_SomeInsert ON SOMETABLE
AFTER INSERT
AS
Insert into someothertable
Select somecolumn from inserted
GO

Does that help at all?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #585369
Posted Tuesday, October 14, 2008 5:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 07, 2009 11:39 PM
Points: 11, Visits: 22
Hi Gail,
Thanks for the reply.

I know that the above trigger will fire after insert action to the sometable and insert the records to someothertable.

You used the statement
Select somecolumn from inserted.

What is this inserted? Can i use the sometable inplace of inserted?
Post #585376
Posted Tuesday, October 14, 2008 6:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 38,079, Visits: 30,374
tochaturvedi (10/14/2008)
Hi Gail,
Thanks for the reply.

I know that the above trigger will fire after insert action to the sometable and insert the records to someothertable.


Just the one column.

You used the statement
Select somecolumn from inserted.

What is this inserted? Can i use the sometable inplace of inserted?


inserted and deleted are pseudo tables visible only within triggers. The inserted table will contain the rows affected by an insert or the new values of rows affected by an update. The deleted table contains rows affected by the delete or the old values of rows affected by an update.

You can use sometable, but then you'll get all the rows in that table. With inserted, you just get the rows that were inserted by the statement that fired the trigger.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #585385
Posted Saturday, October 18, 2008 2:31 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, June 06, 2013 3:51 PM
Points: 68, Visits: 301
I have posted something about this on my blog. Please follow link below:

http://dbalink.wordpress.com/2008/06/20/how-to-sql-server-trigger-101/


Marlon Ribunal
http://marlonribunal.com
Twitter @MarlonRibunal
Post #588160
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse