見た目から、すべてを適切なBEGIN ... ENDステートメントでラップしましたが、コードはほとんどすべてのコード行を通過して実行します。両方の@rows変数に実際に0より大きい値が含まれていることを確認するために、printステートメントも実行しました。誰かが私を正しい方向に向けるのを手伝ってもらえますか?
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