declare @vSQL nvarchar(500), @ColumnName varchar(50), @RunDate datetime, @DateStr char(8)

set @RunDate = '2007-12-25'

while @RunDate > '2006-02-01' begin

set @DateStr = convert(char(8), @RunDate, 112)
print @DateStr + ' 處理中......'

set @vSQL = 'update V2_繳款記錄 set last_in_date = ''' + @DateStr + ''''
+ ' from V2_繳款記錄 where income_' + convert(char(4), convert(int, substring(@DateStr, 1, 6)) - 191100)
+ ' = ''Y'' and last_in_date is null'

print @vSQL

execute sp_executesql @vSQL

set @RunDate = dateadd(month, -1, @RunDate)
continue end


引用自:藍色小惡魔用 SQL 語法動態組合 SQL
http://www.imp.idv.tw/play/forum/viewthread?thread=743

就這樣... 原本要重貼 23 次的 SQL 就這樣取代掉了
SQL 的經典範例都很簡短 大家都是高手 自己玩自己摸應該沒問題 我就不廢話了


arrow
arrow
    文章標籤
    sql mssql sqlserver
    全站熱搜

    藍色小惡魔 發表在 痞客邦 留言(0) 人氣()