在线等一个 关于声明变量的小问题 ---------------

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

/*下面的存储过程:总是报错,提示声明变量:@tbname,但是我已经声明了.不知道为什么?*/  
   
   
   
  create   proc   changspzl   (@spmch1   char(60),  
                                                @spmch2   char(60),  
                                                @chgsp     int,  
                                                @run_result   output)  
  as    
   
  begin  
    declare   @tbname   char(15)     /*已声明变量*/  
      if   @chgsp=1  
      begin   tran  
          declare   tblist   cursor   for  
              select   distinct   tablename   from   tb_stru   where   fieldname=dspname   order   by   tablename  
          open   tblist  
          fetch   first   from   tblist   into   @tbname  
          while   (@@fetch_status=0)    
          begin  
              update   @tbname   set   dspname=@spmch2   where   dspname=@spmch1   /*此行提示需声明变量*/  
              fetch   next   from   tblist   into   @tbname  
          end  
          close   tblist  
          deallocate   tblist  
      if   @@error<>0  
            begin    
                set   @run_result=-1  
                rollback    
            end   else        
            commit   tran  
  end

· 网友精彩回答:

发表者:phantomman

update   @tbname   set   dspname=@spmch2   where   dspname=@spmch1    
   
  表   名称是不能直接使用变量的,要用:  
  execute(update     +   @tbname   +     set......)  
 

发表者:paoluo

改为如下  
   
  create   proc   changspzl   (@spmch1   varchar(60),  
                                                @spmch2   varchar(60),  
                                                @chgsp     int,  
                                                @run_result   output)  
  as    
   
  begin  
    declare   @tbname   varchar(15)     /*已声明变量*/  
      if   @chgsp=1  
      begin   tran  
          declare   tblist   cursor   for  
              select   distinct   tablename   from   tb_stru   where   fieldname=dspname   order   by   tablename  
          open   tblist  
          fetch   first   from   tblist   into   @tbname  
          while   (@@fetch_status=0)    
          begin  
              exec(update   +@tbname   +set   dspname=+@spmch2+   where   dspname=+@spmch1)   /*此行提示需声明变量*/  
              fetch   next   from   tblist   into   @tbname  
          end  
          close   tblist  
          deallocate   tblist  
      if   @@error<>0  
            begin    
                set   @run_result=-1  
                rollback    
            end   else        
            commit   tran  
  end

发表者:libin_ftsafe

create   proc   changspzl   (@spmch1   char(60),  
                                                @spmch2   char(60),  
                                                @chgsp     int,  
                                                @run_result   output)  
  as    
  begin  
      declare   @tbname   char(15)     /*已声明变量*/  
      if   @chgsp=1  
      begin   tran  
          declare   tblist   cursor   for  
                  select   distinct   tablename   from   tb_stru   where   fieldname=dspname   order   by   tablename  
          open   tblist  
          fetch   first   from   tblist   into   @tbname  
          while   (@@fetch_status=0)    
          begin  
              exec(update   +@tbname+   set   dspname=+@spmch2+   where   dspname=+@spmch1+)    
              fetch   next   from   tblist   into   @tbname  
          end  
          close   tblist  
          deallocate   tblist  
      if   @@error<>0  
            begin    
                set   @run_result=-1  
                rollback    
            end   else        
            commit   tran  
  end  
 

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved