我在SPROC中插入语句(简化),如下所示
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
Run Code Online (Sandbox Code Playgroud)
但问题是,在任何操作之后,即使没有更多的行适合我some_condition,@@ROWCOUNT也等于1,而不是0.
当有0行返回匹配我时,如何打破该循环some_condition?
"set"语句创建的行数为1.您应该立即将@@ ROWCOUNT保存到@rowCount变量中并稍后使用该var.
declare @rowCount int
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 @rowCount = @@ROWCOUNT
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
Run Code Online (Sandbox Code Playgroud)
此外,您可以通过将@rowCount初始设置为-1并将WHILE条件更改为@rowCount <> 0来简化.将不再需要条件BREAK.
| 归档时间: |
|
| 查看次数: |
19072 次 |
| 最近记录: |