Sql server ? 这非常令人困惑。如果@rows>'0',如果@rows>0,两个@rows变量都是您有两个不同的变量,一个是文本类型,另一个是数字,都用来跟踪多行吗?这非常令人困惑。 IF @rows > '0' --This agen

Sql server ? 这非常令人困惑。如果@rows>'0',如果@rows>0,两个@rows变量都是您有两个不同的变量,一个是文本类型,另一个是数字,都用来跟踪多行吗?这非常令人困惑。 IF @rows > '0' --This agen,sql-server,tsql,stored-procedures,nested-if,Sql Server,Tsql,Stored Procedures,Nested If,? 这非常令人困惑。如果@rows>'0',如果@rows>0,两个@rows变量都是您有两个不同的变量,一个是文本类型,另一个是数字,都用来跟踪多行吗?这非常令人困惑。 IF @rows > '0' --This agent's Tax ID number has been found to exist in AgentIdentification table BEGIN --Set UniqueAgentId acc


? 这非常令人困惑。
如果@rows>'0'
如果@rows>0
两个@rows变量都是
您有两个不同的变量,一个是文本类型,另一个是数字,都用来跟踪多行吗?这非常令人困惑。
    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
ELSE
    BEGIN TRANSACTION
ELSE
    BEGIN
        BEGIN TRANSACTION
COMMIT TRANSACTION
    COMMIT TRANSACTION
END