將歷史記錄移到歷史表中

2021-06-02 20:52:06 字數 4116 閱讀 6210

--用一配製表存需要轉移資料的大表名稱

--每次都查表名與tab_name相匹配且is_used=1的 說明是當前的歷史表

create table config_table(configid int,             --id

tab_name varchar(64),     --需要轉移資料大表名稱

condition_col varchar(32),--需要移動時間列名稱

history_name varchar(64), --歷史表名稱

is_used int               --是否正在使用

)--   1           test                 test_history             1

create procedure sp_movehistorydata(@tab_name varchar(32),@move_time varchar(12))                      

as--sql

declare @sql           varchar(4000)

--column_name

declare @col_name      varchar(512)

--history column_name

declare @hiscol_name   varchar(512)

--history table_name

declare @histab_name   varchar(32)

declare @condition_col varchar(32)

declare @count         int

begin

select @histab_name = history_name,@condition_col=condition_col from config_table where tab_name=@tab_name and is_used=1

--if there is not history_name then return

if @histab_name is null or @histab_name=''

return

--select source_table column into @col_name

set @sql = n'select @sc_name = left(collist,len(collist)-5)' +

' from ('+

' select object_id,'+

' (select replicate('' '',30-len( [name]))+[name]+'','' from sys.columns where object_id=a.object_id  for xml path('''')) as collist'+

' from sys.columns a where object_id=object_id('''+@tab_name+''')'+

' group by object_id'+

' ) b'

exec sp_executesql @sql,n'@sc_name varchar(512) output',@sc_name=@col_name output

select @count=count(1) from sys.objects where object_id=object_id(@histab_name)

-- if there is not exists history_table then crate and insert data to history_table

-- delete   from   tab_name when match  the  condition

if @count is null or @count ='' or @count =0

begin

set @sql = 'select '+@col_name +' into '+@tab_name+'_his1'+' from '+@tab_name+' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'     

exec (@sql)

set @sql = 'delete from '+@tab_name +' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'

exec (@sql)    

endelse

begin

--select source_table column into @hiscol_name

set @sql = n'select @hc_name = left(collist,len(collist)-5)' +

' from ('+

' select object_id,'+

' (select replicate('' '',30-len( [name]))+[name]+'','' from sys.columns where object_id=a.object_id  for xml path('''')) as collist'+

' from sys.columns a where object_id=object_id('''+@histab_name+''')'+

' group by object_id'+

' ) b'

exec sp_executesql @sql,n'@sc_name varchar(512) output',@hc_name=@hiscol_name output

if @col_name = @hiscol_name

begin

set @sql = 'insert into '+@histab_name+' select '+@col_name +' from '+@tab_name+' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'     

exec (@sql)

set @sql = 'delete from '+@tab_name +' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'

exec (@sql)

endelse

begin

-- get historytable_name 's last char set add one  then rename histab_name

set @count = cast(right(@histab_name,1) as int)

set @count = @count+1

set @histab_name = substring(@histab_name,1,len(@histab_name)-1)+cast(@count as varchar(1))

set @sql = 'select '+@col_name +' into '+@tab_name+'_his1'+' from '+@tab_name+' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'     

exec (@sql)

set @sql = 'delete from '+@tab_name +' where '+@condition_col+' <= convert(datetime,'+@move_time+' 23:59:59 )'

exec (@sql)    

select @count = count(1) from config_table

set @count = @count+1

update config_table set is_used = 0 where tab_name=@tab_name

insert into config_table(configid,tab_name,condition_col, history_name,is_used )

values(@count,@tab_name,@condition_col,@histab_name,1)

endend

end

Better History(歷史記錄)

外掛程式介紹 剛剛看到在網頁上看到一篇好的文章或者正在編輯什麼,不小心一時手賤關掉了網頁,再開啟就找不到了,想必很多朋友經常遇到這樣的事,better history 歷史記錄 讓你更好地檢視您的歷史記錄。為檢視您的歷史記錄帶來最好的搜尋體驗,最清晰的介面和最有幫助的篩選。使用說明 將better ...

MySQL Event歷史記錄

需求 sql server的作業歷史 job 記錄是儲存在msdb庫中的,很方便就查詢相關的job定義,計畫和歷史記錄,而mysql的event卻沒有歷史記錄。為方便檢視event是否正常執行以及執行結果,通過以下兩個步驟來實現類似的功能。實現 1.在mysql庫建立event執行的歷史記錄表 cr...

Ubuntu 檢視歷史記錄

ubuntu 終端的歷史記錄 在linux終端下 1 執行history,可以得到乙個整潔的linux終端命令歷史記錄的列表 2 執行history c,可以清除雜亂的歷史記錄 3 執行history r home yajiu history.txt,可以將history.txt檔案儲存的常用命令讀...