CREATE PROC AccountPersonSelPart
@AccountID varchar(6),
@StrSql varchar(400)  /*存储传过来的查询语句*/
AS
DECLARE @PersonID int
 
EXEC ('declare cur cursor for '+@StrSql)
open Cur
fetch first from Cur into @PersonID
while (@@fetch_status = 0)
begin    
INSERT INTO AccountPerson(AccountID,PersonID) VALUES (@AccountID,@PersonID)
fetch next from Cur into @PersonID
end
close Cur
deallocate Cur
RETURN

解决方案 »

  1.   

    没发现什么问题?
    可检查处:
    1.@strsql 的select 语句提取了多个字段2.@strsql 的select 语法错误
      

  2.   

    --将游标定义语句改用exec就行了.CREATE PROC AccountPersonSelPart
    @AccountID varchar(6),
    @StrSql varchar(400)  /*存储传过来的查询语句*/
    AS
    DECLARE @PersonID int
    exec('declare cur cursor LOCAL scroll 
    for '+@StrSql) /*就是此处有错误,不知该怎么处理*/
    open Cur
    fetch first from Cur into @PersonID
    while (@@fetch_status = 0)
    begin    
    INSERT INTO AccountPerson(AccountID,PersonID) VALUES (@AccountID,@PersonID)
    fetch next from Cur into @PersonID
    end
    close Cur
    deallocate Cur
    RETURN
      

  3.   

    因为默认创建的是全局游标,所以可以在exec外引用exec中定义的游标.
      

  4.   

    在定义游标处使用exec就可以了
    EXEC ('declare cur cursor for '+@StrSql)