[英]SQL stored procedure not working properly
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.
它接受变量,在一个表中进行INSERT,然后使用从该操作创建的主键在单独的表上进行另一次INSERT。
What doesn't happen, however, is the code in the if/else if/else block towards then end. 但是,没有发生的是if / else if / else块中的代码。 It is basically supposed to check to make sure the two ID columns are matching properly.
基本上应该检查以确保两个ID列正确匹配。 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.
如果不是,它将列调整为适当的值,然后返回旧值以及进行输入的人员的日期和用户名(lastChangeOperator)。
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
? 您是否尝试过使用
SCOPE_IDENTITY()
而不是@@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."* *“为了避免以后与添加触发器相关的潜在问题,请始终使用SCOPE_IDENTITY()返回T SQL语句或存储过程中最近添加的行的标识。” *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.