Below is the code for a stored procedure.
The first part of the code works as it is supposed to. It takes the variables, does an INSERT into one table, then with the primary key created from that action does another INSERT on a seperate table.
What doesn't happen, however, is the code in the if/else if/else block towards then end. It is basically supposed to check to make sure the two ID columns are matching properly. If not, it adjusts the columns to their appropriate values, and then returns the old values plus the date and user name (lastChangeOperator) of the person who made the entry.
I've spent all day writing this and another part of this program, so I'm hoping a second set of eyes will do the trick.
ALTER PROCEDURE [dbo].[sp_AgentIdAprCheck]
(
@companyCode char(3),
@agentId char(10),
@firstName nvarChar(50),
@lastname nvarChar(50),
@suffix char(10),
@ssn int,
@taxIdType char(1),
@entityType char(1),
@corporateName nvarChar(100),
@currentUniqueId int OUTPUT,
@currentAgentId int OUTPUT,
@operator nvarchar(50) OUTPUT,
@date datetime OUTPUT
)
AS
DECLARE @rows int = 0
DECLARE @uniqueAgentId int = 0
DECLARE @lastChangeOperator char(6) = 'LVOMQ1'
DECLARE @LastChangeDate datetime
--DECLARE @currentUniqueId int = 0
--DECLARE @currentAgent int = 0
--DECLARE @operator nvarchar(50)
--DECLARE @date datetime
SELECT @rows = COUNT(AgentTaxId)
FROM AgentIdentification
WHERE AgentTaxId = @ssn
if @rows > 0
return 1
else
BEGIN TRANSACTION
SET @LastChangeDate = GETDATE()
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)
DECLARE @uniqueIdRows int = 0
SELECT @uniqueIdRows = COUNT(UniqueAgentId)
FROM UniqueAgentIdToAgentId
WHERE UniqueAgentId = @uniqueAgentId
DECLARE @agentIdRows int = 0
SELECT @agentIdRows = COUNT(AgentId)
FROM UniqueAgentIdToAgentId
WHERE AgentId = @agentId
if @uniqueIdRows = 0 AND @agentIdRows = 0
BEGIN
INSERT INTO UniqueAgentIdToAgentId (UniqueAgentId, AgentId,
CompanyCode, LastChangeOperator, LastChangeDate)
VALUES (@uniqueAgentId, @agentId, @companyCode,
@lastChangeOperator, @LastChangeDate)
END
else if @agentIdRows = 0
BEGIN
SELECT @currentUniqueId = UniqueAgentId,
@operator = LastChangeOperator, @date = @LastChangeDate
FROM UniqueAgentIdToAgentId
UPDATE UniqueAgentIdToAgentId
SET UniqueAgentId = @uniqueAgentId
WHERE AgentId = @agentId AND UniqueAgentId = @currentUniqueId
END
else
BEGIN
SELECT @currentAgentId = AgentId,
@operator = LastChangeOperator, @date = @LastChangeDate
FROM UniqueAgentIdToAgentId
UPDATE UniqueAgentIdToAgentId
SET AgentId = @agentId
WHERE UniqueAgentId = @uniqueAgentId --AND AgentId = @currentAgentId
return 2
END
COMMIT TRANSACTION
Have you tried using SCOPE_IDENTITY()
instead of @@IDENTITY
? I'm guessing this may be your problem.
As Pinal Dave says here
*"To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure."*
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.