逐条更新数据的SQL语句写法


  本文标签:SQL语句

  逐条更新数据的SQL语句可以方便不小心忘记更新数据少加Where语句的朋友,下面就为您介绍逐条更新数据的SQL语句写法,供您参考  。

  1. declare @tid int          
  2. declare @fid int  
  3. declare @i int  
  4. declare @j int  
  5. set @j=(select count(*) from tbl1.dbo.dnt_topics)  
  6. set @i=1           
  7. while   @i<@j           
  8. begin               
  9.     set @tid = (select tid from ( select ROW_NUMBER() over (order by tid asc ) as Row, tid,fid from dnt_topics ) as sp  where Row=@i)  
  10.     set @fid=(select fid from ( select ROW_NUMBER() over (order by tid asc ) as Row, tid,,fid from dnt_topics ) as sp  where Row=@i)                   
  11.     update               
  12.         tbl2.dbo.dnt_topics            
  13.     set                    
  14.         fid=@fid           
  15.     where                
  16.         tid=@tid                       
  17.         set @i=@i+1     

  其中,@tid表示更新限制的条件,@fid表示要更新的数据,定义@i和@j是为了方便方便循环更新,在这T_SQL语句中用了SQL2005自带的函数ROW_NUMBER(),