SQL中類似For循環(huán)處理的實(shí)例
作者:佚名
對SQL數(shù)據(jù)庫的循環(huán)操作可以有多種方法,下面就將為您介紹SQL中類似For循環(huán)處理的實(shí)例,供您參考,希望對您有所啟迪。
原本CreatDate的yyyy-MM-dd hh:mm:ss:fff格式被變成了yyyy-MM-dd格式,下面就將對SQL進(jìn)行類似For循環(huán)處理,該方法供您參考,希望對您學(xué)習(xí)SQL中的For循環(huán)能有所幫助。
- declare @itemnumber int --定義需要循環(huán)的次數(shù)
- declare @tagint int --定義標(biāo)志字段,用于結(jié)束循環(huán)
- set @tagint=1
- select @itemnumber = count(distinct Creater) from Demo_TestTable where isnull(Creater,'')<>'' And
- DATEDIFF(DAY,CreatDate,GETDATE())<1
- if(@itemnumber>0)
- begin
- while @tagint<=@itemnumber
- begin
- waitfor delay '00:00:01' --每隔一秒再執(zhí)行 可用參數(shù)變量替換
- Update Demo_TestTable set CreatDate=GETDATE() where Creater =(
- Select Creater from (
- select Creater,ROW_NUMBER() over(order by Creater) as RowID from Demo_TestTable where
- isnull(Creater,'')<>'' And DATEDIFF(DAY,CreatDate,GETDATE())<1 group by Creater
- ) TableA
- where TableA.RowID=@tagint
- )
- set @tagint=@tagint+1
- end
- end
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)