--update
CREATE PROCEDURE procedure1
(
@id1  nvarchar(20)
)
AS
Begin
  IF Exists (Select * From table1 Where  id = @id1)
    Begin
      declare @tempid nvarchar(20)
      declare mycur cursor for (Select id2 From table1 Where  id = @id1)
      fetch next from mycur into @tempid 
      while @@fetch_status  <> -1
      Begin
        --一些处理
        execute procedure1 @tempid --这里进行迭代
      End
      close mycur
      deallocate mycur
    End
End

解决方案 »

  1.   

    -- 记住游标用完后要关闭(close)、释放(deallocate )
      

  2.   

    WangZWang(先来),你好像只是重贴了一下我的问题哦,是不是贴错了?期待你的帮助:)
      

  3.   

    Try
    CREATE PROCEDURE procedure1
    (
    @id1  nvarchar(20)
    )
    AS
    Begin
      IF Exists (Select * From table1 Where  id = @id1)
        Begin
          declare @tempid nvarchar(20)
          declare @mycur cursor for (Select id2 From table1 Where  id = @id1)  --@开头是局部的
          fetch next from @mycur into @tempid 
          while @@fetch_status  <> -1
          Begin
            --一些处理
            execute procedure1 @tempid --这里进行迭代
            fetch next from @mycur into @tempid                     --必需的
          End
          close @mycur                                             --必需的
          deallocate @mycur                                     --必需的
        End
    End
      

  4.   

    --cursor没有迭代的功能,也没有必要对游标进行迭代
     CREATE PROCEDURE procedure1
    (
    @id1  nvarchar(20)
    )
    AS
    Begin
      IF Exists (Select * From table1 Where  id = @id1)
        Begin
          declare @tempid nvarchar(20)
          declare mycur cursor for (Select id2 From table1 Where  id = @id1)
          fetch next from mycur into @tempid 
          while @@fetch_status =0
          Begin
            --一些处理
            fetch next from mycur into @tempid --执行下一条
          End
          close mycur
          deallocate mycur
        End
    End
      

  5.   

    to i9988(冒牌j9988 V0.1):
    不行啊,修改后会提示“declare @mycur cursor for (Select id2 From table1 Where  id = @id1)  --@开头是局部的”这一行出错。
      

  6.   

    改下,上面错了
    CREATE PROCEDURE procedure1
    (
    @id1  nvarchar(20)
    )
    AS
    Begin
      IF Exists (Select * From table1 Where  id = @id1)
        Begin
          declare @tempid nvarchar(20)
          declare mycur cursor LOCAL for (Select id2 From table1 Where  id = @id1)  --LOCAL是局部的
          fetch next from mycur into @tempid 
          while @@fetch_status  <> -1
          Begin
            --一些处理
            execute procedure1 @tempid --这里进行迭代
            fetch next from mycur into @tempid                     --必需的
          End
          close mycur                                             --必需的
          deallocate mycur                                     --必需的
        End
    End
      

  7.   

    to WangZWang(先来) :
    cursor是没有迭代的功能,我要实现的是存储过程的迭代。现在的问题是我的存储过程中要用到游标。
      

  8.   

    to i9988(冒牌j9988 V0.1):
    似乎没问题了,但出现了新问题:超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)。烦啊。
      

  9.   

    to WangZWang(先来) :
    这个我知道,只是的确还没想到其他方法。先尝试一下吧。谢谢你的忠告:)
      

  10.   

    --------------------------------------------------------------
    您好,我们是“2006中国杰出数据库工程师评选”活动组委会。
    您的帖子已经被我们转载到本次评选官方网站的“专家在线答疑”区。
    http://www.bestdba.cn/match_discussion.aspx在那里,进入本次评选终选的30位数据库工程师将与您展开积极的互动。他们会为您的问题提供满意的答案,此外,您还可以在“专家在线答疑”区提出新的问题并参与讨论。您的帖子位于:
    http://www.bestdba.cn/match_discussion3.aspx?pointid=553&pointid2=1&pointid3=5&pcount=stc
    非常感谢您对本次活动的支持!
      

  11.   

    好了,要结贴了。i9988(冒牌j9988 V0.1)的方案是可行的,谢了。也谢谢WangZWang(先来)的热心帮助,谢谢。