SQL嵌套的IF不起作用

时间:2012-08-17 18:32:50

标签: sql-server tsql stored-procedures nested-if

从它的外观来看,我已经将所有内容包装在正确的BEGIN ... END语句中,但是代码遍历并执行几乎每一行代码。我也做过打印语句,以确保两个@rows变量实际上都包含大于0的值。任何人都可以帮我指出正确的方向吗?

    IF @rows > '0'

        --This agent's Tax ID number has been found to exist in AgentIdentification table
        BEGIN 

            --Set UniqueAgentId according to mapped value from AgentIdentification table
            SELECT @uniqueAgentId = UniqueAgentId
            FROM AgentIdentification
            WHERE AgentTaxId = @ssn

            --Check to make sure this record exists in UniqueAgentIdToAgentId table
            SELECT @rows = COUNT(*)
            FROM UniqueAgentIdToAgentId
            WHERE UniqueAgentId = @uniqueAgentId and AgentId = @agentId

            PRINT @rows
            IF @rows > 0                
                --Record exists in UniqueAgentIdToAgentId table
                --Check to make sure correct UniqueAgentId is mapped to correct AgentId and vice versa
                BEGIN 
                    SELECT @agentIdRows = COUNT(AgentId)
                    FROM UniqueAgentIdToAgentId
                    WHERE UniqueAgentId = @uniqueAgentId and AgentId <> @agentId

                    SELECT @uniqueIdRows = COUNT(UniqueAgentId)
                    FROM UniqueAgentIdToAgentId
                    WHERE AgentId = @agentId and UniqueAgentId <> @uniqueAgentId

                    IF @uniqueIdRows = 0 AND @agentIdRows = 0
                        BEGIN           
                            SET @returnValue = 1
                        END
                    ELSE IF @agentIdRows = 0 AND @uniqueIdRows > 0
                        BEGIN           
                            SET @returnValue = 2
                        END
                    ELSE
                        BEGIN
                            SET @returnValue = 3
                        END
                END

            --Record does not exist in UniqueAgentIdToAgentId and will be added
            ELSE 
                BEGIN
                    INSERT INTO UniqueAgentIdToAgentId (UniqueAgentId, AgentId, CompanyCode, LastChangeOperator, LastChangeDate)
                    VALUES (@uniqueAgentId, @agentId, @companyCode, @lastChangeOperator, @LastChangeDate)
                    SET @returnValue = 4
                END
        END
    ELSE
        BEGIN TRANSACTION

            --This agent Tax ID number does not exist on AgentIdentification table
            --Add record into Agent and AgentIdentification table
            INSERT INTO Agent (EntityType, FirstName, LastName, NameSuffix, CorporateName, LastChangeOperator, LastChangeDate)
            VALUES (@entityType, @firstName, @lastname, '', @corporateName, @lastChangeOperator, @LastChangeDate)

            SELECT @uniqueAgentId = @@IDENTITY
            SELECT UniqueAgentId
            FROM Agent

            INSERT INTO AgentIdentification (UniqueAgentId, TaxIdType, AgentTaxId, LastChangeOperator, LastChangeDate)
            VALUES (@uniqueAgentId, @taxIdType, @ssn, @lastChangeOperator, @lastChangeDate)

            --Check to make sure this record exists in UniqueAgentIdToAgentId table
            SELECT @rows = COUNT(*)
            FROM UniqueAgentIdToAgentId
            WHERE UniqueAgentId = @uniqueAgentId and AgentId = @agentId

            IF @rows > 0
                --Record exists in UniqueAgentIdToAgentId table
                --Check to make sure correct UniqueAgentId is mapped to correct AgentId and vice versa
                BEGIN 
                    SELECT @agentIdRows = COUNT(AgentId)
                    FROM UniqueAgentIdToAgentId
                    WHERE UniqueAgentId = @uniqueAgentId and AgentId <> @agentId

                    SELECT @uniqueIdRows = COUNT(UniqueAgentId)
                    FROM UniqueAgentIdToAgentId
                    WHERE AgentId = @agentId and UniqueAgentId <> @uniqueAgentId

                    IF @uniqueIdRows = 0 AND @agentIdRows = 0
                        BEGIN   
                            SET @returnValue = 5                        
                        END
                    ELSE IF @agentIdRows = 0 AND @uniqueIdRows > 0
                        BEGIN
                            SET @returnValue = 6                        
                        END
                    ELSE
                        BEGIN
                            SET @returnValue = 7                        
                        END
                END

            --Record does not exist in UniqueAgentIdToAgentId and will be added
            ELSE 
                BEGIN
                    INSERT INTO UniqueAgentIdToAgentId (UniqueAgentId, AgentId, CompanyCode, LastChangeOperator, LastChangeDate)
                    VALUES (@uniqueAgentId, @agentId, @companyCode, @lastChangeOperator, @LastChangeDate)
                    SET @returnValue = 8                
                END
    COMMIT TRANSACTION

1 个答案:

答案 0 :(得分:4)

我想这个:

ELSE
    BEGIN TRANSACTION

需要这样:

ELSE
    BEGIN
        BEGIN TRANSACTION

而且:

COMMIT TRANSACTION

需要这样:

    COMMIT TRANSACTION
END
相关问题