问题
I have a update trigger for update status column value change and update other table record and it is working fine. Here is my code
CREATE TRIGGER [dbo].[trgAfterUpdate] ON [dbo].[recharge_request]
FOR UPDATE
AS
DECLARE @status varchar(50);
SELECT @status=i.status FROM inserted i;
IF UPDATE (status)
BEGIN
IF @status='Failure'
--My Update Statement
ELSE IF @status='Success'
--My Update Statement
END
Now I'm want to create an insert trigger also for check status column value and perform other table operation. because in some case status column value will not been update to I need to perform some operation on insert if column value is 'Success
' or 'Fail
'. status column possible values are 'Success
', 'Fail
', 'Pending
' and 'Process
'. any help will be appreciated. Thanks in advance!
回答1:
DECLARE @status varchar(50); SELECT @status=i.status FROM inserted i;
you are not handling cases where multiple rows are being updated.
You need to treat the inserted
and deleted
as a table that may contains more than 1 row
IF UPDATE (status)
BEGIN
update t
set ....
from inserted i
inner join some_table t on i.somecol = t.anothercol
where t.status = 'Failure'
update t
set ....
from inserted i
inner join some_table t on i.somecol = t.anothercol
where t.status = 'Success'
. . . . . -- other status value
END
回答2:
My Issue has been resolved here is solution:
CREATE TRIGGER [dbo].[trgAfterInsert] ON [dbo].[recharge_request]
AFTER INSERT
AS
BEGIN
DECLARE @status varchar(50);
SELECT @status=i.status FROM inserted i;
IF @status='Success'
BEGIN
--My Update statement
END
END
来源:https://stackoverflow.com/questions/50596870/how-to-check-column-value-on-insert-trigger-like-update-trigger