SQL Server存储过程多个IF语句

时间:2016-08-08 19:07:51

标签: sql-server if-statement stored-procedures

我有一个SQL Server存储过程有三个条件。目前,无论参数如何,只会执行第一个IF块:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

/*
DESCRIPTION: Assigns/reassigns orders in [GIS].[dbo].[QCOrders]
PARAMETERS:
    @FldrInitKey    VARCHAR(30) = NULL,
    @UserName       VARCHAR(20) = NULL,
    @QCTrack        DECIMAL(6,2),
    @WF             BIT,
    @idQC           INT         = NULL
RETURNS:
    @RC 0 (success), 1 (failure)
EXAMPLE:
    EXEC spQCAssignOrders '889G1Pt', 'arajendran', '2016.10', '0', NULL
MODIFICATIONS:
    03/10/2016  A.Rajendran Created
*/

ALTER PROCEDURE [dbo].[spQCAssignOrders]
    @FldrInitKey VARCHAR(30) = NULL,
    @UserName    VARCHAR(20) = NULL,
    @QCTrack     DECIMAL(6,2),
    @WF          BIT,
    @idQC        INT = NULL
AS
    SET NOCOUNT ON
    SET ROWCOUNT 0
    SET XACT_ABORT ON

BEGIN TRY
    /* Declare and initialize variables */
    DECLARE @RC INT, @TranCount INT

    SELECT 
        @RC = 0, @TranCount = @@TRANCOUNT

    IF @TranCount = 0 
    BEGIN TRANSACTION
        IF @idQC IS NOT NULL
        BEGIN
            UPDATE [GIS].[dbo].[QCOrders]
            SET UserName = @UserName,
                AssignedDte = CASE  
                                 WHEN @UserName IS NULL 
                                    THEN NULL
                                    ELSE GETDATE()
                              END,
                Unable = NULL,
                Error = NULL,
                CompletedDate = NULL,
                AdminCheck = NULL
            WHERE 
                idQC = @idQC
        END

        IF @FldrInitKey IS NOT NULL
        BEGIN
            UPDATE [GIS].[dbo].[QCOrders]
            SET UserName = @UserName,
                AssignedDte = GETDATE()
            WHERE   
                QCTrack = @QCTrack
                AND FldrInitKey = @FldrInitKey
                AND WellsFargo = @WF
                AND UserName IS NULL
                AND Unable <> 1
        END

        IF @FldrInitKey IS NULL
        BEGIN
            UPDATE [GIS].[dbo].[QCOrders]
            SET UserName = @UserName,
                AssignedDte = GETDATE()
            WHERE   
                QCTrack = @QCTrack
                AND WellsFargo = @WF
                AND UserName IS NULL
                AND Unable <> 1
        END

        IF @TranCount = 0 AND (XACT_STATE()) = 1 
           COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        IF (XACT_STATE()) = -1 
            ROLLBACK TRANSACTION

        EXEC [TOD].[dbo].[spRethrowError]
        SET @RC = 1
    END CATCH

    SET NOCOUNT OFF
    SET ROWCOUNT 0

    RETURN @RC


GRANT EXECUTE ON [GIS].[dbo].[spQCAssignOrders] TO db_allowexec

如果我想执行此操作:

EXEC spQCAssignOrders '889G1Pt', 'arajendran', '2016.10', '0', NULL

第二个IF语句应该执行,但事实并非如此。为什么呢?

澄清

第二次IF声明:

IF @FldrInitKey IS NOT NULL
BEGIN
    UPDATE [GIS].[dbo].[QCOrders] 
    SET UserName = @UserName,
        AssignedDte = GETDATE()
    WHERE  
        QCTrack = @QCTrack
        AND FldrInitKey = @FldrInitKey
        AND WellsFargo = @WF
        AND UserName IS NULL
        AND Unable <> 1
END

1 个答案:

答案 0 :(得分:1)

你可以大大简化这一点。所有三个更新语句都可以转换为单个更新语句。这应该相当于所有三个。

UPDATE [GIS].[dbo].[QCOrders]
                SET 
                    UserName    = @UserName,
                    AssignedDte = 
                    CASE    
                        WHEN @idQC IS NOT NULL and @UserName IS NULL THEN NULL
                        ELSE GETDATE()
                    END

            WHERE   QCTrack         = @QCTrack
                    AND FldrInitKey = ISNULL(@FldrInitKey, FldrInitKey)
                    AND WellsFargo  = @WF
                    AND UserName IS NULL
                    AND Unable <> 1