Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Triggers Examples


Triggers Examples

Author
Message
tochaturvedi-908256
tochaturvedi-908256
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 29
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.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47343 Visits: 44392
What are you trying to do with triggers?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
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


tochaturvedi-908256
tochaturvedi-908256
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 29
The trigger should insert records into tables.
I need only general examples.
Chandra Sekhara Vyas Dhara
Chandra Sekhara Vyas Dhara
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3963 Visits: 1149
Check the following link

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



GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47343 Visits: 44392
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, MVP, M.Sc (Comp Sci)
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


tochaturvedi-908256
tochaturvedi-908256
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 29
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?
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47343 Visits: 44392
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, MVP, M.Sc (Comp Sci)
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


MarlonRibunal
MarlonRibunal
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 355
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
ganesh.khuspe_v
ganesh.khuspe_v
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
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????
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1378 Visits: 2597
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
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