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 12»»

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-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:30 PM
Points: 41,531, Visits: 34,448
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: Monday, September 30, 2013 2:09 AM
Points: 3,131, Visits: 1,058
Check the following link

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



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


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:30 PM
Points: 41,531, Visits: 34,448
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-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:30 PM
Points: 41,531, Visits: 34,448
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: Monday, January 27, 2014 2:02 PM
Points: 69, Visits: 312
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
Posted Wednesday, April 02, 2014 4:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 03, 2014 4:32 AM
Points: 4, Visits: 9
Hi all,
following is my trigger:

CREATE TRIGGER trigger1 ON Table1 after Update AS
begin
INSERT INTO table2
SELECT * from inserted
end


in above Query i want to add record from inserted table to table2 which is on another Database2.
and Table1 is in Database1.
After Updating Database1.Table1 trigger should get fire and record get inserted into Database2.Table2.
How can we do this????
Post #1557401
Posted Wednesday, April 02, 2014 5:10 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: 2 days ago @ 6:52 AM
Points: 470, Visits: 823
ganesh.khuspe_v (4/2/2014)
Hi all,
following is my trigger:

CREATE TRIGGER trigger1 ON Table1 after Update AS
begin
INSERT INTO table2
SELECT * from inserted
end


in above Query i want to add record from inserted table to table2 which is on another Database2.
and Table1 is in Database1.
After Updating Database1.Table1 trigger should get fire and record get inserted into Database2.Table2.
How can we do this????


you need to do the following


CREATE TRIGGER trigger1 ON Table1 after Update AS
begin
INSERT INTO [Database2].[schema].table2(ID, Name) ---- this is required otherwise will get error.
SELECT ID, Name
from inserted
end

and table2 must exists in the Database2 on the same sql server.

hope it helps
Post #1557403
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse