作業(yè)介紹
SQL SERVER的作業(yè)是一系列由SQL SERVER代理按順序執(zhí)行的指定操作,。作業(yè)可以執(zhí)行一系列活動(dòng),,包括運(yùn)行Transact-SQL腳本、命令行應(yīng)用程序,、Microsoft ActiveX腳本,、Integration Services 包、Analysis Services 命令和查詢(xún)或復(fù)制任務(wù),。作業(yè)可以運(yùn)行重復(fù)任務(wù)或那些可計(jì)劃的任務(wù),,它們可以通過(guò)生成警報(bào)來(lái)自動(dòng)通知用戶(hù)作業(yè)狀態(tài),從而極大地簡(jiǎn)化了 SQL Server 管理[參見(jiàn)MSDN],。
創(chuàng)建作業(yè),、刪除作業(yè)、查看作業(yè)歷史記錄....等所有操作都可以通過(guò)SSMS管理工具GUI界面操作,,有時(shí)候也確實(shí)挺方便的,。但是當(dāng)一個(gè)實(shí)例有多個(gè)作業(yè)或多個(gè)數(shù)據(jù)庫(kù)實(shí)例時(shí),通過(guò)圖形化的界面去管理,、維護(hù)作業(yè)也是個(gè)頭痛的問(wèn)題,,對(duì)于SQL腳本與GUI界面管理維護(hù)作業(yè)熟優(yōu)熟劣這個(gè)問(wèn)題,只能說(shuō)要看場(chǎng)合,。下面主要介紹通過(guò)SQL腳本來(lái)管理,、維護(hù)作業(yè)。
作業(yè)分類(lèi)
創(chuàng)建作業(yè)時(shí),,往往需要指定作業(yè)類(lèi)別,,如果不指定新建作業(yè)類(lèi)別,就會(huì)默認(rèn)為“[未分類(lèi)(本地)]”,,如下截圖所示:
當(dāng)然,,你可以查看、添加,、刪除,、修改作業(yè)分類(lèi)。請(qǐng)看下面操作,。
1:查看作業(yè)分類(lèi)
Code Snippet
- --method 1:
- EXEC msdb.dbo.sp_help_category;
-
- GO
-
- --method 2:
- SELECT category_id ,--作業(yè)類(lèi)別ID
- category_class ,--類(lèi)別中項(xiàng)目類(lèi)型:1=作業(yè)2=警報(bào) 3=操作員
- category_type ,--類(lèi)別中類(lèi)型:=本地,、=多服務(wù)器、=無(wú)
- name --分類(lèi)名稱(chēng)
- FROMmsdb.dbo.syscategories
有興趣的可以研究一下存儲(chǔ)過(guò)程msdb.dbo.sp_help_category
sp_help_category
2:添加作業(yè)分類(lèi)
如下所示,,添加一個(gè)叫"DBA_MONITORING"的作業(yè)分類(lèi)
Code Snippet
- EXEC msdb.dbo.sp_add_category
- @class=N'JOB',
- @type=N'LOCAL',
- @name=N'DBA_MONITORING' ;
-
- GO
-
-
-
- SELECT * FROM msdb.dbo.syscategories WHERE NAME='DBA_MONITORING'
-
- category_id category_class category_type name
- ----------- -------------- ------------- -------------
- 102 1 1 DBA_MONITORING
有興趣的可以研究一下存儲(chǔ)過(guò)程msdb.dbo.sp_add_category
sp_add_category
3:刪除作業(yè)分類(lèi)
如下所示,,刪除一個(gè)叫"DBA_MONITORING" 的作業(yè)分類(lèi)
Code Snippet
- EXEC msdb.dbo.sp_delete_category
- @name = N'DBA_MONITORING',
- @class = N'JOB' ;
-
- GO
有興趣的可以研究一下存儲(chǔ)過(guò)程msdb.dbo.sp_delete_category
sp_delete_category
4:修改作業(yè)類(lèi)別
msdb.dbo.sp_update_category
[@class =] 'class' ,
[@name =] 'old_name' ,
[@new_name =] 'new_name'
|
有興趣的可以研究一下存儲(chǔ)過(guò)程msdb.dbo.sp_update_category
sp_update_category
分析上面四個(gè)存儲(chǔ)過(guò)程可以看出,實(shí)質(zhì)上新增,、修改,、刪除、查看作業(yè)類(lèi)別無(wú)非就是對(duì)表 msdb.dbo.syscategories進(jìn)行操作,,只是通過(guò)存儲(chǔ)過(guò)程封裝了而已,,增加了驗(yàn)證等操作,確保數(shù)據(jù)完整性,。
新建作業(yè)
創(chuàng)建作業(yè)的步驟一般如下所示:
- 執(zhí)行 sp_add_job 來(lái)創(chuàng)建作業(yè),。
-
執(zhí)行 sp_add_jobstep 來(lái)創(chuàng)建一個(gè)或多個(gè)作業(yè)步驟,。
-
執(zhí)行 sp_add_schedule 來(lái)創(chuàng)建計(jì)劃。
-
執(zhí)行 sp_attach_schedule 將計(jì)劃附加到作業(yè),。
-
執(zhí)行 sp_add_jobserver 來(lái)設(shè)置作業(yè)的服務(wù)器,。
本地作業(yè)是由本地 SQL Server 代理進(jìn)行緩存的。因此,,任何修改都會(huì)隱式強(qiáng)制 SQL Server 代理重新緩存該作業(yè),。由于直到調(diào)用 sp_add_jobserver 時(shí),SQL Server 代理才緩存作業(yè),,因此最后調(diào)用 sp_add_jobserver 將更為有效,。
下面看用腳本新建一個(gè)作業(yè)用來(lái)每天執(zhí)行exec sp_cycle_errorlog ,實(shí)現(xiàn)錯(cuò)誤日志循環(huán),, 從下面的腳本量來(lái)看,,用腳本新建一個(gè)作業(yè)確實(shí)工作量很大,而且容易出錯(cuò),,GUI圖形界面創(chuàng)建作業(yè)要方便得多,,但是如果遷移數(shù)據(jù)庫(kù)時(shí),用腳本來(lái)新建作業(yè)是相當(dāng)方便的,。比GUI圖形界面新建一個(gè)作業(yè)快捷方便多了,。
Code Snippet
- USE [msdb]
- GO
-
- /****** Object: Job [JOB_CYCLE_ERRORLOG] Script Date: 08/23/2013 15:25:09 ******/
- IFEXISTS(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'JOB_CYCLE_ERRORLOG')
- EXEC msdb.dbo.sp_delete_job@job_id=N'a5dff08b-95f8-498e-a6c9-59241fe197b4', @delete_unused_schedule=1
- GO
-
- USE [msdb]
- GO
-
- /****** Object: Job [JOB_CYCLE_ERRORLOG] Script Date: 08/23/2013 15:25:09 ******/
- BEGIN TRANSACTION
- DECLARE @ReturnCode INT
- SELECT @ReturnCode = 0
- /****** Object: JobCategory [DBA_MATIANCE] Script Date: 08/23/2013 15:25:09 ******/
- IF NOT EXISTS(SELECT name FROM msdb.dbo.syscategories WHERE name=N'DBA_MATIANCE' AND category_class=1)
- BEGIN
- EXEC @ReturnCode = msdb.dbo.sp_add_category@class=N'JOB', @type=N'LOCAL', @name=N'DBA_MATIANCE'
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
-
- END
-
- DECLARE @jobId BINARY(16)
- EXEC @ReturnCode =msdb.dbo.sp_add_job@job_name=N'JOB_CYCLE_ERRORLOG',
- @enabled=1,
- @notify_level_eventlog=0,
- @notify_level_email=0,
- @notify_level_netsend=0,
- @notify_level_page=0,
- @delete_level=0,
- @description=N'每天執(zhí)行exec sp_cycle_errorlog 實(shí)現(xiàn)錯(cuò)誤日志循環(huán)。',
- @category_name=N'DBA_MATIANCE',
- @owner_login_name=N'sa', @job_id = @jobId OUTPUT
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** Object: Step [Step 1: recycle the errorlog] Script Date: 08/23/2013 15:25:09 ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep@job_id=@jobId, @step_name=N'Step 1: recycle the errorlog',
- @step_id=1,
- @cmdexec_success_code=0,
- @on_success_action=1,
- @on_success_step_id=0,
- @on_fail_action=2,
- @on_fail_step_id=0,
- @retry_attempts=0,
- @retry_interval=0,
- @os_run_priority=0, @subsystem=N'TSQL',
- @command=N'exec msdb.dbo.sp_cycle_errorlog',
- @database_name=N'msdb',
- @flags=0
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Job Schedule',
- @enabled=1,
- @freq_type=4,
- @freq_interval=1,
- @freq_subday_type=1,
- @freq_subday_interval=0,
- @freq_relative_interval=0,
- @freq_recurrence_factor=0,
- @active_start_date=20130823,
- @active_end_date=99991231,
- @active_start_time=0,
- @active_end_time=235959,
- @schedule_uid=N'2099c694-cd26-4edf-8803-179227bf8770'
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- COMMIT TRANSACTION
- GOTO EndSave
- QuitWithRollback:
- IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
- EndSave:
-
- GO
作業(yè)系統(tǒng)表
與作業(yè)有關(guān)的系統(tǒng)表,、視圖大致有下面9個(gè),,下面就不費(fèi)口舌詳細(xì)解說(shuō)每一個(gè)系統(tǒng)表的作用了,MSDN文檔上有詳細(xì)的解說(shuō),,有興趣的翻看一下即可,。
SELECT * FROM msdb.dbo.sysjobs --存儲(chǔ)將由 SQL Server 代理執(zhí)行的各個(gè)預(yù)定作業(yè)的信息
SELECT * FROM msdb.dbo.sysjobschedules --包含將由 SQL Server 代理執(zhí)行的作業(yè)的計(jì)劃信息
SELECT * FROM msdb.dbo.sysjobactivity; --記錄當(dāng)前 SQL Server 代理作業(yè)活動(dòng)和狀態(tài)
SELECT * FROM msdb.dbo.sysjobservers --存儲(chǔ)特定作業(yè)與一個(gè)或多個(gè)目標(biāo)服務(wù)器的關(guān)聯(lián)或關(guān)系
SELECT * FROM msdb.dbo.sysjobsteps; --包含 SQL Server 代理要執(zhí)行的作業(yè)中的各個(gè)步驟的信息
SELECT * FROM msdb.dbo.sysjobstepslogs; --包含所有 SQL Server 代理作業(yè)步驟的作業(yè)步驟日志
SELECT * FROM msdb.dbo.sysjobs_view; --
SELECT * FROM msdb.dbo.sysjobhistory --包含有關(guān) SQL Server 代理執(zhí)行預(yù)定作業(yè)的信息
SELECT * FROM msdb.dbo.syscategories --包含由 SQL Server Management Studio 用來(lái)組織作業(yè)、警報(bào)和操作員的類(lèi)別
運(yùn)行作業(yè)
啟動(dòng)作業(yè)
1:通過(guò)SSMS工具啟動(dòng)作業(yè)[參見(jiàn)MSDN]
2:通過(guò)SQL命令啟動(dòng)作業(yè)
啟動(dòng)作業(yè)一般通過(guò)sp_start_job來(lái)實(shí)現(xiàn),,具體語(yǔ)法與操作見(jiàn)下面,。
語(yǔ)法:
sp_start_job
{ [@job_name =] 'job_name'
| [@job_id =] job_id }
[ , [@error_flag =] error_flag]
[ , [@server_name =] 'server_name']
[ , [@step_name =] 'step_name']
[ , [@output_flag =] output_flag]
例子:
exec msdb.dbo.sp_start_job @job_name='JOB_CYCLE_ERRORLOG'
停止作業(yè)
1:通過(guò)SSMS工具停作業(yè)[參見(jiàn)MSDN]
2:通過(guò)SQL命令停止作業(yè)
語(yǔ)法:
sp_stop_job
[@job_name =] 'job_name'
| [@job_id =] job_id
| [@originating_server =] 'master_server'
| [@server_name =] 'target_server'
例子:
exec msdb.dbo.sp_stop_job @job_name='JOB_CYCLE_ERRORLOG'
啟用或禁用作業(yè)
1:通過(guò)SSMS工具啟用作業(yè)[參見(jiàn)MSDN]
2:通過(guò)SQL命令禁用作業(yè)
語(yǔ)法:
sp_update_job [ @job_id =] job_id | [@job_name =] 'job_name'
[, [@new_name =] 'new_name' ]
[, [@enabled =] enabled ]
[, [@description =] 'description' ]
[, [@start_step_id =] step_id ]
[, [@category_name =] 'category' ]
[, [@owner_login_name =] 'login' ]
[, [@notify_level_eventlog =] eventlog_level ]
[, [@notify_level_email =] email_level ]
[, [@notify_level_netsend =] netsend_level ]
[, [@notify_level_page =] page_level ]
[, [@notify_email_operator_name =] 'email_name' ]
[, [@notify_netsend_operator_name =] 'netsend_operator' ]
[, [@notify_page_operator_name =] 'page_operator' ]
[, [@delete_level =] delete_level ]
[, [@automatic_post =] automatic_post ]
列子:
EXEC msdb.dbo.sp_update_job
@job_name = N'JOB_CYCLE_ERRORLOG',
@enabled = 0 ; --0 禁用作業(yè)、 1啟用作業(yè)
GO
刪除作業(yè)
1:通過(guò)SSMS工具刪除作業(yè)[參見(jiàn)MSDN]
2:通過(guò)SQL命令刪除作業(yè)
語(yǔ)法:
sp_delete_job { [ @job_id = ] job_id | [ @job_name = ] 'job_name' } , [ , [ @originating_server = ] 'server' ] [ , [ @delete_history = ] delete_history ] [ , [ @delete_unused_schedule = ] delete_unused_schedule ]
例子:
EXEC msdb.dbo.sp_delete_job @job_name = 'JOB_CYCLE_ERRORLOG';
遷移作業(yè)
使用 Transact-SQL 編寫(xiě)作業(yè)腳本
-
在對(duì)象資源管理器中,,連接到 Microsoft SQL Server 數(shù)據(jù)庫(kù)引擎實(shí)例,,再展開(kāi)該實(shí)例。
-
展開(kāi)“SQL Server 代理”,,再展開(kāi)“作業(yè)”,然后右鍵單擊要編寫(xiě)腳本的作業(yè),。
-
從快捷菜單中,,選擇“編寫(xiě)作業(yè)腳本為”,再選擇“CREATE 到”或“DROP 到”,,并單擊下列內(nèi)容之一:
新查詢(xún)編輯器窗口,,將打開(kāi)一個(gè)新的查詢(xún)編輯器窗口,,并為其編寫(xiě) Transact-SQL 腳本。
文件,,將 Transact-SQL 腳本保存到文件,。
剪貼板,將 Transact-SQL 腳本保存到剪貼板
常用管理作業(yè)SQL
1:查看屬于某個(gè)數(shù)據(jù)庫(kù)的所有作業(yè),。
Code Snippet
- SELECT j.job_id AS JOB_ID ,
- name AS JOB_NAME ,
- enabled AS JOB_ENABLED ,
- description AS JOB_DESCRIPTION ,
- date_created AS DATE_CREATED ,
- date_modified AS DATE_MODIFIED
- FROM msdb.dbo.sysjobs j
- WHERE job_id IN( SELECTjob_id
- FROM msdb.dbo.sysjobsteps
- WHERE database_name = 'DataBaseName' )
2:查看某個(gè)作業(yè)類(lèi)別的所有作業(yè)
Code Snippet
- SELECT j.name AS Job_Name ,
- j.description AS Job_Description ,
- j.date_created AS Date_Created ,
- j.date_modified AS Date_Modified ,
- c.name AS Job_Class
- FROM msdb.dbo.sysjobs j
- LEFT JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
- WHEREc.name = '[Uncategorized (Local)]'
3:查看禁用/啟用的作業(yè)
SELECT * FROM msdb.dbo.sysjobs WHERE enabled=0 --0:禁用 1:為啟用
4:查看出錯(cuò)的作業(yè)記錄
4.1:查詢(xún)那些作業(yè)在今天出錯(cuò)(如果要查詢(xún)歷史出錯(cuò)作業(yè),,去掉查詢(xún)時(shí)間條件即可)
Code Snippet
- SELECT name AS JOB_NAME ,
- description AS JOB_Description ,
- date_created AS Date_Created ,
- date_modified AS Date_Modified
- FROM msdb.dbo.sysjobs
- WHERE enabled = 1
- AND job_id IN(
- SELECT job_id
- FROM Msdb.dbo.sysjobhistory
- WHERE run_status = 0
- AND run_date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT) )
4.2:查看出錯(cuò)詳細(xì)信息
Code Snippet
- SELECT j.name AS JOB_NAME ,
- h.step_id AS STEP_ID ,
- h.step_name AS STEP_NAME,
- h.message AS ERR_MSG ,
- h.run_date AS RUN_DATE ,
- h.run_time AS RUN_TIME ,
- msdb.dbo.agent_datetime(h.run_date, h.run_time) AS 'RunDateTime' ,
- CAST(run_duration / 10000 AS VARCHAR(2)) + N'小時(shí)'
- + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2))
- + N'分鐘' + SUBSTRING(CAST(run_duration AS VARCHAR(10)),
- LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)
- + N'秒' AS run_duration
- FROM msdb.dbo.sysjobhistory h
- LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
- WHERE run_status = 0
- AND run_date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)
5:查看作業(yè)的執(zhí)行時(shí)間:
5.1:查看當(dāng)天成功執(zhí)行的作業(yè)的時(shí)間(查看的是作業(yè)Step信息)
Code Snippet
- SELECT j.name AS job_name ,
- h.step_id AS step_id ,
- h.step_name AS step_name,
- h.message AS Message ,
- h.run_date AS Run_date ,
- h.run_time AS run_time ,
- msdb.dbo.agent_datetime(h.run_date, h.run_time) AS 'RunDateTime' ,
- CAST(run_duration / 10000 AS VARCHAR(2)) + N'小時(shí)'
- + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2))
- + N'分鐘' + SUBSTRING(CAST(run_duration AS VARCHAR(10)),
- LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)
- + N'秒' AS run_duration
- FROM msdb.dbo.sysjobhistory h
- LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
- WHERE run_status = 1
- AND run_date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)
- ORDER BY run_duration DESC
5.2:查詢(xún)每個(gè)作業(yè)的執(zhí)行時(shí)間、按執(zhí)行時(shí)間降序
Code Snippet
- SELECT j.name AS JOB_NAME ,
- h.run_date AS RUN_DATE ,
- SUM(run_duration) AS SUM_DURATION
- FROM msdb.dbo.sysjobhistory h
- LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
- WHERE run_status = 1
- AND run_date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)
- GROUP BY name ,
- run_date
- ORDER BY Sum_Duration DESC
參考資料:
http://blog.csdn.net/dba_huangzj/article/details/8300178
|