码迷,mamicode.com
首页 > 数据库 > 详细

SQL 在循环内修改字段值

时间:2015-05-29 12:05:26      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:sql技巧

在网站找一段,发现并没有达到自己的要求:

这段程序功能是 为某空字段自动编号(系统运用时,新加的编号字段,前期有数据,需要重新编号)

declare @id int

declare @maxid int
set @id=1
select @maxid=count(*) from t_Send_Revisit Where fSO is null
begin
 while @id<=@maxid
begin
update s set fSO =(Select ‘SR‘ +CONVERT(varchar(100), GETDATE(), 112)+ ‘-‘ +RIGHT(‘0000‘+convert(varchar,@id),4)) from t_Send_Revisit s
inner join (select top 1 *  From t_Send_Revisit Where fSO is null )  t on t.SSO=s.SSO and t.STime = s.STime and t.fSNo= s.fSNo and s.CustCode = t.CustCode 
set @id=@id+1
end
end

SQL 在循环内修改字段值

标签:sql技巧

原文地址:http://blog.csdn.net/hsl_hxx/article/details/46226369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!