欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

创建数据库作业

程序员文章站 2022-05-02 21:01:04
...

通过SQL语句来创建步骤,警报,计划,通知,目标等来执行一个作业。 无 USE [msdb]GO/****** Object: Job [DatabaWorkFlowSql] Script Date: 2014/11/17 19:48:02 ******/BEGIN TRANSACTIONDECLARE @ReturnCode INTSELECT @ReturnCode = 0/****** Object: Job

通过SQL语句来创建步骤,警报,计划,通知,目标等来执行一个作业。
USE [msdb]
GO

/****** Object:  Job [DatabaWorkFlowSql]    Script Date: 2014/11/17 19:48:02 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 2014/11/17 19:48:02 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR  0 OR @ReturnCode  0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'DatabaWorkFlowSql', 
		@enabled=1, 
		@notify_level_eventlog=1, 
		@notify_level_email=1, 
		@notify_level_netsend=1, 
		@notify_level_page=1, 
		@delete_level=0, 
		@description=N'日志传送的说明内容。
1.在TableInfo里面创建一个表
2.插入数据
3.备份数据库', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'ROBORTLY\勇', 
		@notify_email_operator_name=N'luoyong', 
		@notify_netsend_operator_name=N'luoyong', 
		@notify_page_operator_name=N'luoyong', @job_id = @jobId OUTPUT
IF (@@ERROR  0 OR @ReturnCode  0) GOTO QuitWithRollback
/****** Object:  Step [createTable]    Script Date: 2014/11/17 19:48:02 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'createTable', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@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'create table WorkFlowTable
(
	wfid  int,
	datas  nvarchar(10)
)', 
		@database_name=N'TableInfo', 
		@flags=0
IF (@@ERROR  0 OR @ReturnCode  0) GOTO QuitWithRollback
/****** Object:  Step [insertValues]    Script Date: 2014/11/17 19:48:02 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'insertValues', 
		@step_id=2, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@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'insert into workflowTable values(1,''luoyong'')', 
		@database_name=N'TableInfo', 
		@flags=0
IF (@@ERROR  0 OR @ReturnCode  0) GOTO QuitWithRollback
/****** Object:  Step [备份]    Script Date: 2014/11/17 19:48:02 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'备份', 
		@step_id=3, 
		@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'BACKUP DATABASE [TableInfo] TO  [luoyongImportant] WITH NOFORMAT, NOINIT,  NAME = N''TableInfo-完整 数据库 备份'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO', 
		@database_name=N'TableInfo', 
		@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'Plan', 
		@enabled=1, 
		@freq_type=1, 
		@freq_interval=0, 
		@freq_subday_type=0, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=0, 
		@active_start_date=20141117, 
		@active_end_date=99991231, 
		@active_start_time=194600, 
		@active_end_time=235959, 
		@schedule_uid=N'10456071-9a44-4c6a-82b7-7243899c4db6'
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