@@fetch_status 这个值通常用来判断取回数据的状态对吧。可如果我写
while (@@fetch_status = 0)
begin
(SQL操作)
end
他所影响的行就比我预期的多一行,但结果正确。
而我写成
while (@@fetch_status <> -1)
begin
IF (@@fetch_status <> -2)
BEGIN
(sql操作)
end
END
影响的行数就正常了,结果也正常。
为什么呢,只要成功了,@@fetch_status不就是0么?不过我见高手都是用第二种方式的。能不能详细解释一下其中的原理。谢谢
while (@@fetch_status = 0)
begin
(SQL操作)
end
他所影响的行就比我预期的多一行,但结果正确。
而我写成
while (@@fetch_status <> -1)
begin
IF (@@fetch_status <> -2)
BEGIN
(sql操作)
end
END
影响的行数就正常了,结果也正常。
为什么呢,只要成功了,@@fetch_status不就是0么?不过我见高手都是用第二种方式的。能不能详细解释一下其中的原理。谢谢
...
DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName FROM Northwind.dbo.Employees
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor