How to check column value on insert trigger like Update trigger

杀马特。学长 韩版系。学妹 提交于 2020-03-25 22:34:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!