在我的 sql 代码中,我声明了一个表(A)并从数据库表(B)中向该表中插入了一些行。然后我必须从 A 中取出那些插入的行并将其放入 CURSOR 中,然后在我执行完之后FETCH NEXT
仍然是@@FETCH_STATUS
is -1
。但 的期望值@@FETCH_STATUS
为0
。我在问题下面放了一个简化的代码。
我能知道这段代码有什么问题吗?我可以使用声明的表来填充CURSOR
SQL 中的 s 还是必须从数据库中创建的表中填充游标。
// This is a code that goes inside a Stored Procedure.
AS
DECLARE A TABLE (.........)// A table has same fields in table B
DECLARE s INT
WHILE EXISTS ( SELECT * FROM B WHERE ......)
BEGIN
BEGIN TRAN
INSERT INTO A SELECT TOP 10 (....)FROM B WITH (UPDLOCK, HOLDLOCK) WHERE ....
SELECT s = count(*) from A // this returns some value which means inserting is working
DECLARE dataSet CURSOR FOR (SELECT..... FROM A)
OPEN dataSet
FETCH NEXT FROM dataSet INTO ...
WHILE @@FETCH_STATUS = 0 // coming value for this is -1
BEGIN
//Code goes here
FETCH NEXT FROM dataSet INTO ...
END
CLOSE dataSet
DEALLOCATE dataSet
DELETE FROM A
COMMIT TRAN
END