我有insert語句(簡體)像下面TSQL中斷循環ROWCOUNT = 0
SET ROWCOUNT 100
WHILE(1=1)
BEGIN
INSERT INTO table1
SELECT *
FROM table2
WHERE some_condition
-- EDIT: Realized forgot to include this following vital line that is causing issue
SET @var = @var + @@ROWCOUNT
-- @@ROWCOUNT now takes on a value of 1, which will cause the following IF check to fail even when no lines are inserted
IF(@@ROWCOUNT = 0)
BEGIN
BREAK
END
END
一個存儲過程,但問題是,任何操作後,即使沒有更多的行適合我some_condition
,@@ROWCOUNT
等於1
,而不是0
。
當有0行返回匹配我的some_condition
時,我該如何中斷該循環?
當你說這是「簡化」時,你的實際代碼是否可以在INSERT和@@ ROWCOUNT的測試之間進行任何設置@@ ROWCOUNT值?在INSERT後立即捕獲int變量中的@@ ROWCOUNT值並在測試中使用該變量可能是最安全的。我不確定Sybase,但在SQL Server中,SET ROWCOUNT已被棄用,建議使用SELECT TOP(100)... – GilM 2013-03-22 22:24:09
您發佈的代碼有一個BEGIN和兩個END,所以IF和BREAK都在外面你的WHILE循環。如何發佈有效的代碼? – 2013-03-23 02:14:43