存储过程的规范是:从我的表中
选择并返回(顺序并不重要,只要找到的前 1 个就可以了),一旦我选择了该记录,就需要对其进行标记,以免它被选中再次。Id
tb_r12028dxi_SandpitConsoleProofClient
'P'
这是存储过程:
ALTER PROCEDURE [dbo].[r12028dxi_SandpitConsoleProofSweep]
@myId INT OUTPUT
AS
/*
DECLARE @X INT
EXECUTE [xxx].[dbo].[r12028dxi_SandpitConsoleProofSweep] @X OUTPUT
SELECT @X
*/
DECLARE @NumQueue INT = (
SELECT [cnt] = COUNT(*)
FROM xxx.DBO.tb_r12028dxi_SandpitConsoleProofClient
WHERE [Status] IS NULL
);
IF @NumQueue > 0
BEGIN
BEGIN TRANSACTION;
DECLARE @foundID INT = (SELECT TOP 1 Id FROM xxx.DBO.tb_r12028dxi_SandpitConsoleProofClient WHERE [Status] IS NULL);
UPDATE x
SET x.[Status] = 'P'
FROM xxx.DBO.tb_r12028dxi_SandpitConsoleProofClient x
WHERE x.Id = @foundID
SET @myId = @foundID;
RETURN;
COMMIT TRANSACTION;
END;
GO
它正在返回错误消息:
EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 语句的数量不匹配。先前计数 = 0,当前计数 = 1。
我刚刚添加了Update
脚本,BEGIN TRANSACTION;
并且COMMIT TRANSACTION;
在此之前它看起来像下面这样运行良好......
ALTER PROCEDURE [dbo].[r12028dxi_SandpitConsoleProofSweep]
@myId INT OUTPUT
AS
/*
DECLARE @X INT
EXECUTE [xxx].[dbo].[r12028dxi_SandpitConsoleProofSweep] @X OUTPUT
SELECT @X
*/
DECLARE @NumQueue INT = (
SELECT [cnt] = COUNT(*)
FROM xxx.DBO.tb_r12028dxi_SandpitConsoleProofClient
WHERE [Status] IS NULL
);
IF @NumQueue > 0
BEGIN
SELECT TOP 1 @myId = Id FROM xxx.DBO.tb_r12028dxi_SandpitConsoleProofClient;
RETURN;
END;
GO
我添加了BEGIN TRANSACTION;
/COMMIT TRANSACTION;
因为我想确保数据被读入输出变量并且更新发生。我应该省略程序的这一部分吗?