|
Example of turning boss
IF EXISTS (SELECT * FROM msdb.dbo.sysjobs WHERE name = 'Enable pubs database')
EXEC msdb.dbo.sp_delete_job @ job_name = 'Enable pubs database'
--Define creation job
DECLARE @jobid uniqueidentifier
EXEC msdb.dbo.sp_add_job
@job_name = N 'Enable the pubs database',
@job_id = @jobid OUTPUT
-Define job steps
DECLARE @sql nvarchar (400), @ dbname sysname
SELECT @ dbname = N'master ', --Online or offline database can only be performed in the master database
@ sql = N'ALTER DATABASE pubs SET ONLINE '-use pubs database online (enable)
EXEC msdb.dbo.sp_add_jobstep
@job_id = @jobid,
@step_name = N 'Enable pubs database processing',
@subsystem = 'TSQL',
@ database_name = @ dbname,
@command = @sql
--Create scheduling (using several job scheduling templates specifically defined later)
EXEC msdb..sp_add_jobschedule
@job_id = @jobid,
@name = N 'Enable pubs database processing scheduling',
@ freq_type = 4,
@ freq_interval = 1,
@ freq_subday_type = 0x1,
@ freq_subday_interval = 1,
@active_start_time = 075000-every day at 07:50
--Add target server
DECLARE @servername sysname
SET @ servername = CONVERT (nvarchar (128), SERVERPROPERTY (N'ServerName '))
EXEC msdb.dbo.sp_add_jobserver
@job_id = @jobid,
@server_name = @servername |
|