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


SQL Trigger on condition


SQL Trigger on condition

Author
Message
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
Hi Team,

i need a trigger based on condition.



CREATE TRIGGER [dbo].[Trigger_Base_line] on [dbo].[Base_Tab]
AFTER INSERT,DELETE,UPDATE


here i want a condition logic in trigger,

if a record is inserted into base_tab
then
Execute Stored_procedure1


if a record is deleted from base_tab
then
Execute Stored_procedure2


if a record is updated in base_tab
then
Execute Stored_procedure3


please help me...
Bhaskar.Shetty
Bhaskar.Shetty
Right there with Babe
Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)

Group: General Forum Members
Points: 778 Visits: 509
Instead of triggers you can use OUTPUT Parameter, Like Below.

--- For Insert ---
INSERT INTO TableName
OUTPUT INSERTED.* INTO ToTableName
VALUES
---
--- For Update ---
UPDATE TableName SET ColumnToupdate
OUTPUT deleted.* INTO ToUpdTableName
---
--- For Delete ---
DELETE FROM TableName
OUTPUT deleted.* INTO ToDelTableName
WHERE
---



I suggested using OUTPUT because your trigger is ALTER INSERT, UPDATE, DELETE it mince you want to update this after the data inserted/updated/deleted.
sqlnaive
sqlnaive
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3687 Visits: 2774
Why not try three different triggers rather than using one ?

If you insist on making just one trigger based on all events, here is what you can do inside the trigger:


IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_UPDATE
END
IF EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_INSERT
END
IF NOT EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_DELETE
END


Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
Thank u,

my requirement is when ever insert happens on main table then i want to execute Stored proc1,
if delete happens on main table then execute stored proc2,
if update then stored proc3,

how to know what operation (insert/delete/update) happens on main table.
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
Thank U sqlnaive

Hope it works.

Great...!
Bhaskar.Shetty
Bhaskar.Shetty
Right there with Babe
Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)

Group: General Forum Members
Points: 778 Visits: 509
sqlnaive (5/23/2013)
Why not try three different triggers rather than using one ?

If you insist on making just one trigger based on all events, here is what you can do inside the trigger:


IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_UPDATE
END
IF EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_INSERT
END
IF NOT EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
EXEC PROC_FOR_DELETE
END


Insert Happens mince do you mean by AFTER INSERT OR INSTEAD OF INSERT, if it is after insert you can create three seprate triggers or suggsted in previous post or use output parameter as I described , If it is INSTEAD OF INSERT the mail insert will not fire in its place you can update other table as required.
sqlnaive
sqlnaive
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3687 Visits: 2774
Bhaskar.Shetty (5/23/2013)
[quote]Insert Happens mince do you mean by AFTER INSERT OR INSTEAD OF INSERT, if it is after insert you can create three seprate triggers or suggsted in previous post or use output parameter as I described , If it is INSTEAD OF INSERT the mail insert will not fire in its place you can update other table as required.


Just play it simple. It's upto your choice what you want to achieve. I just gave two options. :-)

In between, would love to see how it worked.
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