简体   繁体   English

如何在插入触发器(如更新触发器)上检查列值

[英]How to check column value on insert trigger like Update trigger

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 '. 因为在某些情况下,状态列值不会更新为,如果列值为' Success '或' Fail ',我需要对插入执行一些操作。 status column possible values are ' Success ', ' Fail ', ' Pending ' and ' Process '. 状态列的可能值为“ Success ”,“ Fail ”,“ Pending ”和“ Process ”。 any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!

 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 您需要将inserteddeleted的表视为可能包含多于1行的表

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM