1

从外观上看,我已经将所有内容都包含在正确的 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
4

1 回答 1

4

我认为这:

ELSE
    BEGIN TRANSACTION

需要是这样的:

ELSE
    BEGIN
        BEGIN TRANSACTION

和这个:

COMMIT TRANSACTION

需要是这样的:

    COMMIT TRANSACTION
END
于 2012-08-17T18:57:01.033 回答