I have a trigger like this
CREATE TRIGGER [TR_INSREC] ON [dbo].[SUB_MIS]
FOR INSERT, UPDATE
AS
IF <insert operation>
logic1
ELSE IF <update operation>
logic2
END IF
END
Now I want to know how can I get whether the trigger fires for insert or update operation. Because for insert, I put logic1 and for update, I put logic2...
Kindly help.....
You have to use Inserted & Deleted spl tables, these trigger scoped tables both table structure will be same as your main base table.
For Insert:
Only Inserted Table have data
Deleted Table will be empty
For Update:
Both Inserted & Deleted table have data
For Delete:
Inserted Table will be EMPTY
Only Deleted Table have data
Code Snippet
CREATE TRIGGER TR_INSREC ON dbo.SUB_MIS FOR INSERT, UPDATE, DELETE
AS
Begin
Declare @.InsertCount as Int;
Declare @.DeleteCount as Int;
Select @.InsertCount =0, @.DeleteCount=0
Select Top 1 @.InsertCount = 1 From Inserted;
Select Top 1 @.DeleteCount = 1 From Deleted;
IF @.InsertCount=1 And @.DeleteCount=0
Begin
--Your Insert logic1
Print 'Insert'
End
IF @.InsertCount=1 And @.DeleteCount=1
Begin
--Your Update Logic
Print 'Update'
End
IF @.InsertCount=0 And @.DeleteCount=1
Begin
--Your Delete Logic
Print 'Delete'
End
End
|||Hi subhendude,
if you have different logic, why don;t you simply make 2 triggers - one for insert and one for delete?
No comments:
Post a Comment