在使用@@ROWCOUNT 的 while 语句中,try catch 发生了什么变化?
What does a try catch change in a while statement that uses @@ROWCOUNT?
我想在 while 循环中添加一个 try catch。在 @@ROWCOUNT > 0 时使用的循环。在 while 我有一个 update top (100) 语句,它运行良好,没有 try catch 绕过它。当我添加 try 时,while 在第一个循环中结束。 try 对 @@ROWCOUNT 有什么影响,使 while 循环结束甚至难以更新触及 100 条记录?
--do we have anything to process?
select top 1 * from SomeTable where processedFlag is null
WHILE(@@ROWCOUNT > 0)
BEGIN
begin try
-- here I have an udpate top (100) statement that processes records with null flag in small batches
end try
begin catch
-- update @@ROWCOUNT so the while continues?
select top 1 * from SomeTable where processedFlag is null
end catch
END
我相信是因为
Statements such as USE, SET , DEALLOCATE CURSOR, CLOSE CURSOR,
BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.
可能END TRY
在其中,但MSDN
没有列出所有可能的陈述。
这将解决问题:
DECLARE @i INT
SELECT @i = COUNT(*) FROM SomeTable WHERE processedFlag IS NULL
WHILE(@i > 0)
BEGIN
BEGIN TRY
UPDATE...
SET @i = @@ROWCOUNT
END TRY
BEGIN CATCH
SELECT @i = COUNT(*) FROM SomeTable WHERE processedFlag IS NULL
END CATCH
END
我想在 while 循环中添加一个 try catch。在 @@ROWCOUNT > 0 时使用的循环。在 while 我有一个 update top (100) 语句,它运行良好,没有 try catch 绕过它。当我添加 try 时,while 在第一个循环中结束。 try 对 @@ROWCOUNT 有什么影响,使 while 循环结束甚至难以更新触及 100 条记录?
--do we have anything to process?
select top 1 * from SomeTable where processedFlag is null
WHILE(@@ROWCOUNT > 0)
BEGIN
begin try
-- here I have an udpate top (100) statement that processes records with null flag in small batches
end try
begin catch
-- update @@ROWCOUNT so the while continues?
select top 1 * from SomeTable where processedFlag is null
end catch
END
我相信是因为
Statements such as USE, SET , DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.
可能END TRY
在其中,但MSDN
没有列出所有可能的陈述。
这将解决问题:
DECLARE @i INT
SELECT @i = COUNT(*) FROM SomeTable WHERE processedFlag IS NULL
WHILE(@i > 0)
BEGIN
BEGIN TRY
UPDATE...
SET @i = @@ROWCOUNT
END TRY
BEGIN CATCH
SELECT @i = COUNT(*) FROM SomeTable WHERE processedFlag IS NULL
END CATCH
END