0

我已经用谷歌搜索过,但我没有找到我的问题的答案。在互联网上,一切都说我做得对。我的问题是 - 我现在正在尝试将我保存到光标的值写入(是的,仅此而已)。但是我使用的循环是无穷大的,它从头到尾从光标写入所有值,一次又一次地......

CREATE PROCEDURE CreateOrEditClient(...Parameters...) AS
DECLARE c CURSOR FOR SELECT column FROM table;
DECLARE @wrt VARCHAR(20);
DECLARE @tmp INT = 0;
 BEGIN
  OPEN c;
   FETCH NEXT FROM c INTO @wrt;
    WHILE @@FETCH_STATUS = 0
     BEGIN
      print CAST(@tmp AS VARCHAR(10)) + '  ' + @wrt;
      SET @tmp = @tmp +1;
      FETCH NEXT FROM c INTO @wrt;
     END;
  CLOSE c;
  DEALLOCATE c;
END;

EXEC CreateOrEditClient ...;

在我看来,游标写得正确,但输出是:

0  790710/1112
1  900519/5555
2  790716/7877
....
19 111111/1111
0  790710/1112
1  900519/5555
....

最后它会写入错误消息“超出最大存储过程、函数、触发器或视图嵌套级别(限制 32)”。

如果我只使用

SELECT column FROM table;

它只写入 20 条记录。

我知道这个例子有更好的解决方案,但我需要知道为什么光标不起作用。将来可能会有用。感谢您的每一个回答。

4

4 回答 4

1

好的,在有问题的评论中是解决方案。

“执行程序名称……;” 编译时不能像过程的主体一样在同一个文件中。

CREATE PROCEDURE CreateOrEditClient(...Parameters...) AS
DECLARE c CURSOR FOR SELECT column FROM table;
DECLARE @wrt VARCHAR(20);
DECLARE @tmp INT = 0;
 BEGIN
  OPEN c;
   FETCH NEXT FROM c INTO @wrt;
    WHILE @@FETCH_STATUS = 0
 BEGIN
  print CAST(@tmp AS VARCHAR(10)) + '  ' + @wrt;
    SET @tmp = @tmp +1;
    FETCH NEXT FROM c INTO @wrt;
 END;
  CLOSE c;
  DEALLOCATE c;
END;

有问题的示例将递归调用过程。谢谢大家。

于 2013-05-03T20:52:04.527 回答
0

请试试这个...

BEGIN
    SET NOCOUNT ON ;
    DECLARE @wrt VARCHAR(20)
    DECLARE @tmp INT = 0

    DECLARE Cur_Common CURSOR  FAST_FORWARD READ_ONLY FOR   
    SELECT iID FROM 'YourTable'
    OPEN Cur_Common
    FETCH NEXT FROM Cur_Common INTO @wrt

    WHILE @@FETCH_STATUS = 0 
        BEGIN
            print CAST(@tmp AS VARCHAR(10)) + '  ' + @wrt;
SET @tmp = @tmp +1;

       FETCH NEXT FROM Cur_Common INTO @wrt
        END
        CLOSE Cur_Common
    DEALLOCATE Cur_Common
END
于 2013-05-03T10:36:06.840 回答
0

利用

 DECLARE @tmp INT
 SET @tmp=0

代替 DECLARE @tmp INT = 0

于 2013-05-03T12:59:18.003 回答
0

您可能会注意到您是否会正确缩进代码。你有:

CREATE PROCEDURE CreateOrEditClient(...Parameters...) AS
DECLARE c CURSOR FOR SELECT column FROM table;
DECLARE @wrt VARCHAR(20);
DECLARE @tmp INT = 0;
BEGIN
    OPEN c;
        FETCH NEXT FROM c INTO @wrt;
        WHILE @@FETCH_STATUS = 0
        BEGIN
            print CAST(@tmp AS VARCHAR(10)) + '  ' + @wrt;
            SET @tmp = @tmp +1;
            FETCH NEXT FROM c INTO @wrt;
        END;
    CLOSE c;
    DEALLOCATE c;
END;

你应该有:

CREATE PROCEDURE CreateOrEditClient(...Parameters...) AS
BEGIN
    DECLARE @wrt VARCHAR(20);
    DECLARE @tmp INT = 0;
    DECLARE c CURSOR FOR SELECT column FROM table;
    OPEN c;
        FETCH NEXT FROM c INTO @wrt;
        WHILE @@FETCH_STATUS = 0
        BEGIN
            print CAST(@tmp AS VARCHAR(10)) + '  ' + @wrt;
            SET @tmp = @tmp +1;
            FETCH NEXT FROM c INTO @wrt;
        END;
    CLOSE c;
    DEALLOCATE c;
END;

EXEC CreateOrEditClient ...;

顺便提一句。你的代码甚至运行了吗?对我来说似乎是一个语法错误。

编辑:哦,参数未在括号中声明 AFAIK。例如:

CREATE PROCEDURE tmp_mol_cleanup_db
    @param1 type,
    @param2 type
AS
BEGIN
    -- procedure body
END;
于 2013-05-03T10:33:09.787 回答