自动安装Sql Jobs Sql Server

时间:2010-02-19 13:48:18

标签: sql-server

有没有办法通过脚本在Sql Server上安装Sql Jobs?我希望在客户远程设置数据库时安装sql作业。这可能吗?

1 个答案:

答案 0 :(得分:2)

如果您想要添加SQL代理作业,则只需要查看msdb中的sp_add_job(step | schedule | server)存储过程,BOL

中有相当好的描述。

另一方面,如果您正在谈论确保安装,运行等代理,那么我不相信有办法通过脚本来实现这一点。

如果,第三方面,你在谈论某个特定的产品/数据库,在谈论SQLJobs时,那么这个答案根本无法帮助你。

修改

获得正确调用的最简单方法(再次,如果我们正在讨论SQL代理作业)是在本地计算机上构建它们,然后通过SSMS / EM编写脚本。我发现最大的问题是你需要为一个作业添加一个服务器,即使它只是一个本地服务器工作。

编辑2

响应第一条评论 - 您对sp_add_jobserver的调用可以将服务器名称指定为“(本地)”,这将使其成为本地服务器作业。

这是一个简短但完整的(希望仍在使用)脚本,可以在一夜之间自动恢复数据库。请注意,它没有引用它所在的服务器,因此将作业添加到它运行的任何服务器上:

USE [msdb]
GO

BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'RESTORE_DatabaseN', 
        @enabled=1, 
        @notify_level_eventlog=2, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=N'No description available.', 
        @category_name=N'Database Maintenance', 
        @owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'step 1', 
        @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=1, 
        @os_run_priority=0, @subsystem=N'TSQL', 
        @command=N'DECLARE @BakFile varchar(120),  
        @OLD_DB sysname,
        @NEW_DB sysname,
        @RestoreData sysname,
        @RestoreLog sysname,
        @DBLogical varchar(255),
        @LogLogical varchar(255),

SET @OLD_DB     = ''DatabaseN''
SET @NEW_DB      = ''DatabaseN''
SET @BakFile = ''\\remoteserver\Backups\Server2\DatabaseN\DatabaseN.BAK''

SET @RestoreData = ''E:\sqldata\'' + @NEW_DB + ''.mdf''
SET @RestoreLog  = ''F:\SQLLogs\'' + @NEW_DB + ''_log.ldf''

create table #filelist (LogicalName varchar(255), PhysicalName varchar(255), Type varchar(20), FileGroupName varchar(255), Size varchar(20), MaxSize varchar(20) )
insert #filelist exec (''RESTORE FILELISTONLY FROM disk = '''''' + @BakFile +  '''''''')
select @DBLogical = LogicalName from #filelist where Type = ''D''
select @LogLogical = LogicalName from #filelist where Type = ''L''

RESTORE DATABASE @NEW_DB FROM DISK = @BakFile
WITH MOVE @DBLogical TO @RestoreData,
MOVE @LogLogical TO @RestoreLog, REPLACE
EXEC ('' USE '' + @NEW_DB + '' BACKUP LOG '' + @NEW_DB + '' WITH Truncate_Only'')

EXEC ('' USE '' + @NEW_DB + '' ALTER DATABASE '' + @NEW_DB + '' SET RECOVERY SIMPLE'')

--EXEC ('' USE '' + @NEW_DB + '' dbcc shrinkfile('' + @LogLogical + '', 2000)'')
', 
        @database_name=N'master', 
        @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'daily', 
        @enabled=1, 
        @freq_type=8, 
        @freq_interval=127, 
        @freq_subday_type=1, 
        @freq_subday_interval=0, 
        @freq_relative_interval=0, 
        @freq_recurrence_factor=1, 
        @active_start_date=20070708, 
        @active_end_date=99991231, 
        @active_start_time=11500, 
        @active_end_time=235959
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