SQL 在迴圈內修改欄位值,sql迴圈修改欄位

來源:互聯網
上載者:User

SQL 在迴圈內修改欄位值,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

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.