根據表中記錄的變化情況自動維護作業

2021-04-16 13:12:46 字數 3113 閱讀 8567

/*--作業處理例項

?根據sendtab的sendtime定製作業

?並且在該作業完成時,可以自動刪除作業

--*/

--示例

--測試表

create table sendtab(id int identity(1,1),name varchar(10)

?,sendtime datetime,acceptunit varchar(10)

?,sendunit varchar(10),content varchar(8000))

create table acceptetab(id int identity(1,1),name varchar(10)

?,sendunit varchar(10),acceptunit varchar(10),content varchar(8000))

go--建立處理的儲存過程

create proc p_jobset

@id int,???--要處理的sendtab的id

@is_delete bit=0?--是否僅刪除,為0則否,為1則是

asdeclare @dbname sysname,@jobname sysname

?,@date int,@time int

select @jobname='定時傳送作業_'+cast(@id as varchar)

?,@date=convert(varchar,sendtime,112)

?,@time=replace(convert(varchar,sendtime,108),':','')

from sendtab where id=@id

if exists(select 1 from msdb..sysjobs where name=@jobname)

?exec msdb..sp_delete_job @job_name=@jobname

if @is_delete=1 return

--建立作業

exec msdb..sp_add_job @job_name=@jobname,@delete_level=1

--建立作業步驟

declare @sql varchar(800)

select @sql='insert acceptetab(name,sendunit,acceptunit,content)

??select name,acceptunit,sendunit,content from sendtab where id='

??+cast(@id as varchar)

?,@dbname=db_name()

exec msdb..sp_add_jobstep @job_name=@jobname,

?@step_name = '傳送處理步驟',

?@subsystem = 'tsql',

?@database_name=@dbname,

?@command = @sql,

?@retry_attempts = 5, ??--重試次數

?@retry_interval = 5? ??--重試間隔

--建立排程

exec msdb..sp_add_jobschedule @job_name = @jobname,

?@name = '時間安排',

?@enabled = 1,

?@freq_type = 1,

?@active_start_date = @date,

?@active_start_time = @time

-- 新增目標伺服器

exec msdb.dbo.sp_add_jobserver

?@job_name = @jobname ,

?@server_name = n'(local)'

go--建立處理的觸發器(新增/修改)

create trigger tr_insert_update on sendtab

for insert,update

asdeclare @id int

declare tb cursor local for select id from inserted

open tb

fetch next from tb into @id

while @@fetch_status=0

begin

?exec p_jobset @id

?fetch next from tb into @id

endclose tb

deallocate tb

go--建立處理的觸發器(刪除)

create trigger tr_delete on sendtab

for delete

asdeclare @id int

declare tb cursor local for select id from deleted

open tb

fetch next from tb into @id

while @@fetch_status=0

begin

?exec p_jobset @id,1

?fetch next from tb into @id

endclose tb

deallocate tb

go--測試

--插入資料

insert sendtab

select '文書1','2004/5/1 12:00:00','unita','unitb','txt'

union all select '文書2','2004/5/12 12:00:00','unita','unitb','txt'

union all select '文書3','2004/5/21 12:00:00','unita','unitb','txt'

--修改

update sendtab set name='檔案1',sendtime='2004/5/1 15:00:00'

where id=1

--刪除

delete sendtab where id=3

go--刪除測試

drop table sendtab,acceptetab

drop proc p_jobset

根據表中記錄的變化情況自動維護作業

作業處理例項 根據sendtab的sendtime定製作業 並且在該作業完成時,可以自動刪除作業 示例 測試表 create table sendtab id int identity 1,1 name varchar 10 sendtime datetime,acceptunit varchar ...

根據表中記錄的變化情況自動維護作業

作業處理例項 根據sendtab的sendtime定製作業 並且在該作業完成時,可以自動刪除作業 示例 測試表 create table sendtab id int identity 1,1 name varchar 10 sendtime datetime,acceptunit varchar ...

根據表中記錄的變化情況自動維護作業

作業處理例項 根據sendtab的sendtime定製作業 並且在該作業完成時,可以自動刪除作業 示例 測試表 create table sendtab id int identity 1,1 name varchar 10 sendtime datetime,acceptunit varchar ...