逐条更新数据的SQL语句可以方便不小心忘记更新数据少加Where语句的朋友,下面就为您介绍逐条更新数据的SQL语句写法,供您参考。
- declare @tid int
- declare @fid int
- declare @i int
- declare @j int
- set @j=(select count(*) from tbl1.dbo.dnt_topics)
- set @i=1
- while @i<@j
- begin
- 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)
- 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)
- update
- tbl2.dbo.dnt_topics
- set
- fid=@fid
- where
- tid=@tid
- set @i=@i+1
其中,@tid表示更新限制的条件,@fid表示要更新的数据,定义@i和@j是为了方便方便循环更新,在这T_SQL语句中用了SQL2005自带的函数ROW_NUMBER(),
【编辑推荐】
教您如何进行SQL跨表更新