try (did not test, so it might not work)WHILE @@FETCH_STATUS = 0
BEGIN
   declare @Y char(01)
   declare @chgtabname sysname     SET @Y = substring(@chgwo,5,1)
   if substring(@chgwo,6,1) > '6'
      SET @chgtabname = 'pack0'+@Y+'b_log'
   else
      SET @chgtabname = 'pack0'+@Y+'b_log'
exec('insert into @tmptab  select a.wono,a.partno,a.woqty as    woqty,b.okqty from woms a, (select wono,count(*) as okqty from ' + @chgtabname  + ' where  ispass =1 and rework=0 and wono=''' + @chgwo + ''' and ispass=1 and rework=0 group by wono ) b where a.wono=b.wono')          fetch next from chgwo_cursor
   into @chgwo
 END 

解决方案 »

  1.   

    fetch next from chgwo_cursor
    into @chgwo
    这两句放在你的位置就会跳过第一行记录,象saucer(思归, MS .NET MVP)说的那样改一下试试。
      

  2.   

    毛病肯定出在
    ...'insert into @tmptab  select ...
    会爆出不能识别变量@tmptab 
    去查查前面的帖子,已经讨论的很详细了。
      

  3.   

    declare      @tabname  char(10)
    declare     @chgwo    nchar(10)
    declare      @tmptab   table (wono char(10),partno char(20),woqty dec,okqty dec)declare chgwo_cursor CURSOR global FOR 
    select distinct wono from changewo where process = 'pack' and  wono like 'WO%'
    open chgwo_cursor 
    fetch chgwo_cursor into @chgwo

    WHILE @@FETCH_STATUS = 0
    BEGIN
       declare @Y char(01)
       declare @chgtabname sysname 
       
       into @chgwo
       SET @Y = substring(@chgwo,5,1)
       if substring(@chgwo,6,1) > '6'
          SET @chgtabname = 'pack0'+@Y+'b_log'
       else
          SET @chgtabname = 'pack0'+@Y+'b_log'
    exec('insert into @tmptab  select a.wono,a.partno,a.woqty as    woqty,b.okqty from woms a, (select wono,count(*) as okqty from ' + @chgtabname  + ' where  ispass =1 and rework=0 and wono=''" + @chgwo + "'
    ' and ispass=1 and rework=0 group by wono ) b where a.wono=b.wono')
     END 
    fetch chgwo_cursor into @chgwo
    CLOSE chgwo_cursor
    deallocate chgwo_cursor
      

  4.   

    把" + @chgwo + "改成' + @chgwo + '试一下