触发应该是,但不是,递归,对吧?

时间:2014-07-25 18:06:49

标签: sql sql-server-2008 tsql recursion triggers

触发器的新功能,并尝试弄清楚为什么嵌套IF在此触发器的结尾处似乎不是递归的。我想要的是它一次又一次地触发它直到它达到@ProcessStatus的4.它只会变为1.不确定为什么,有什么建议吗?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[tr_ProcessRecord] ON [dbo].[_ShipMain]
FOR UPDATE
AS
BEGIN TRANSACTION

    declare @cqID nvarchar(24),
            @carrier nvarchar(24),
            @shipCode nvarchar(24),
            @Note nvarchar(255),
            @ProcessStatus nvarchar(24);

    SELECT @cqID = i.ConnectQueryID from inserted i;
    SELECT @carrier = i.Carrier from inserted i;
    SELECT @shipCode = i.ShipCode from inserted i;
    SELECT @ProcessStatus = i.Status from inserted i;
    SELECT @Note = 'ID: ' + @cqID + ' ' +
                   'Carrier: ' + ' ' + convert(nvarchar(24), @carrier) + ' ' +
                   'Ship Code: ' + @shipCode + ' ' +
                   (SELECT CASE @ProcessStatus
                        WHEN 0 THEN 'Recieved'
                        WHEN 1 THEN 'Processing'
                        WHEN 2 THEN 'More Processing'
                        WHEN 3 THEN 'Finishing'
                        WHEN 4 THEN 'Processed'
                    END);

    INSERT INTO [_ShipLog]
    SELECT @cqID
          ,GETDATE()
          ,@Note
          ,@ProcessStatus

    IF @ProcessStatus = 0
    BEGIN
        UPDATE [_ShipMain]
        SET [Status] = @ProcessStatus + 1
        WHERE [ConnectQueryID] = @cqID;
    END
    IF @ProcessStatus = 1
    BEGIN
        UPDATE [_ShipMain]
        SET [Status] = @ProcessStatus + 1
        WHERE [ConnectQueryID] = @cqID;
    END
    IF @ProcessStatus = 2
    BEGIN
        UPDATE [_ShipMain]
        SET [Status] = @ProcessStatus + 1
        WHERE [ConnectQueryID] = @cqID;
    END
    IF @ProcessStatus = 3
    BEGIN
        UPDATE [_ShipMain]
        SET [Status] = @ProcessStatus + 1
        WHERE [ConnectQueryID] = @cqID;
    END

COMMIT TRANSACTION

1 个答案:

答案 0 :(得分:1)

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[tr_ProcessRecord] ON [dbo].[_ShipMain]
FOR UPDATE
AS
BEGIN TRANSACTION

    declare @cqID nvarchar(24),
            @carrier nvarchar(24),
            @shipCode nvarchar(24),
            @Note nvarchar(255),
            @ProcessStatus nvarchar(24);

    SELECT 
            @cqID = i.ConnectQueryID
            ,@carrier = i.Carrier
            ,@shipCode = i.ShipCode
            ,@ProcessStatus = i.Status 
        from inserted i;
    SELECT @Note = 'ID: ' + @cqID + ' ' +
                   'Carrier: ' + ' ' + convert(nvarchar(24), @carrier) + ' ' +
                   'Ship Code: ' + @shipCode + ' ' +
                   (SELECT CASE @ProcessStatus
                        WHEN 0 THEN 'Recieved'
                        WHEN 1 THEN 'Processing'
                        WHEN 2 THEN 'More Processing'
                        WHEN 3 THEN 'Finishing'
                        WHEN 4 THEN 'Processed'
                    END);

    INSERT INTO [_ShipLog]
    SELECT @cqID
          ,GETDATE()
          ,@Note
          ,@ProcessStatus

UPDATE [_ShipMain]
        SET [Status] = @ProcessStatus + 1
        WHERE [ConnectQueryID] = @cqID
        and @ProcessStatus in (0,1,2,3)

COMMIT TRANSACTION