exec('
DECLARE  authors_cursor CURSOR  OPTIMISTIC DYNAMIC FOR  
select * from Fcashdocitemt where doccode=@doccode
for update of docitem')
-->试试
DECLARE  authors_cursor CURSOR  OPTIMISTIC DYNAMIC FOR  
select * from Fcashdocitemt where doccode=@doccode
for update of docitem

解决方案 »

  1.   

    try:create proc mypro
    @doccode varchar(20)
    as
    DECLARE @i int,@sql varchar(500)
    set @i=0
    set @sql=' DECLARE  authors_cursor CURSOR  OPTIMISTIC DYNAMIC FOR  '+
             ' select * from Fcashdocitemt where doccode='''+@doccode+''''+
             ' for update of docitem'
    exec(@sql)
    open authors_cursorfetch first from authors_cursor
    while (@@fetch_status=0)begin
    select @i=@i+1update Fcashdocitem set [docitem]=@i where
    CURRENT OF authors_cursor
    fetch next from authors_cursorend close authors_cursordeallocate authors_cursor
      

  2.   

    一个exec就是一个事务.
    在这个事务中你定义了authors_cursor这个局部游标,又使用了另一个事务定义的@doccode变量这样的话,你肯定会出错的啊.你为什么要用动态sql语句呢?觉得不就也OK啊.
    create proc mypro
    @doccode varchar(20)
    as
    DECLARE @i int
    set @i=0
    DECLARE  authors_cursor CURSOR  OPTIMISTIC DYNAMIC FOR  
    select * from Fcashdocitemt where doccode=@doccode
    for update of docitemopen authors_cursorfetch first from authors_cursor
    while (@@fetch_status=0)begin
    select @i=@i+1update Fcashdocitem set [docitem]=@i where
    CURRENT OF authors_cursor
    fetch next from authors_cursorend close authors_cursordeallocate authors_cursor
      

  3.   

    你在exec('字符串')语法时,里面如用到了量应采用‘+’连接,最上面第一位和第二位都是对的,我就不写代码了。
      

  4.   

    你在exec('字符串')语法时,里面如用到了变量(@doccode)应采用‘+’连接,最上面第一位和第二位都是对的,我就不写代码了。