• 利用代码生成一个SQL数据库维护计划,并启用它


    利用系统存储过程:
    sp_add_maintenance_plan
    添加维护计划并返回该计划 ID。
    sp_add_maintenance_plan_db
    将数据库和维护计划相关联。
    sp_delete_maintenance_plan
    删除指定的维护计划。
    sp_delete_maintenance_plan_db
    取消指定数据库和指定维护计划的关联。

     


    String sql= " DECLARE @DBName SYSNAME, @LogFileID SMALLINT, @TagLogSize SMALLINT "
    " DECLARE @PlanID UNIQUEIDENTIFIER, @iResult INT              "
    "DECLARE @PlanName VARCHAR(128)                               "
    " DECLARE @JobID1 BINARY(16), @JobID2 BINARY(16)               "
    " DECLARE @JobName SYSNAME, @JobCommand NVARCHAR(3200)         "
    " DECLARE @path VARCHAR(128) "
    " DECLARE @back VARCHAR(10) "
    " DECLARE @back_2 VARCHAR(10) "
    " SET @DBName = 'netbar' "
    " SET @TagLogSize = 256 "
    " SET @path='D:\\BACK' "
    " SET @back='BAK' "
    " SET @back_2='TRN'                                        "
                                                          
    " USE msdb                                                     "

    " SET @PlanName = @DBName + ' Maintenance Plan'                    "

    " EXECUTE @iResult = sp_add_maintenance_plan @PlanName, @PlanID OUTPUT  "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                     "
    " BEGIN                                                                   "
    " PRINT '不能建立维护计划'                                          "
    " RETURN                                                            "
    " END                                                                     "

    " EXECUTE @iResult = sp_add_maintenance_plan_db @PlanID, @DBName          "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                      "
    " BEGIN                                                                   "
    " PRINT '不能把数据库关联到维护计划'                                 "
    "  RETURN                                                            "
    " END                                                                     "
                                                                        
    " SET @JobName = @PlanName + N'的完全备份作业'                             "

    " SET @JobCommand = N'EXECUTE master.dbo.xp_sqlmaint N''-PlanID ' + CONVERT(VARCHAR(40), @PlanID) +       "
    " '  -VrfyBackup -BkUpMedia '+@path+' -BkUpDB  -UseDefDir  -DelBkUps 1WEEKS -BkExt '+@back+'''' "

    " EXECUTE @iResult = sp_add_job @job_id = @JobID1 OUTPUT ,                  "
    " @job_name = @JobName,                                               "
    "  @owner_login_name = N'sa',                                          "
    " @description = N'No description available.',                        "
    "  @category_name = NULL,                                              "
    " @enabled = 1,                                                       "
    " @notify_level_email = 0,                                            "
    " @notify_level_page = 0,                                             "
    " @notify_level_netsend = 0,                                          "
    " @notify_level_eventlog = 2,                                         "
    " @delete_level= 0                                                    "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                        "
    " BEGIN                                                                     "
    " PRINT '不能建立完全备份的作业'                                      "
    " RETURN                                                              "
    " END                                                                       "
    " EXECUTE @iResult = sp_add_jobstep @job_id = @JobID1,                      "
    " @step_id = 1,                                                       "
    " @step_name = N'第 1 步',                                            "
    " @command = @JobCommand,                                             "
    " @database_name = N'master',                                         "
    " @server = N'',                                                      "
    " @database_user_name = N'',                                          "
    " @subsystem = N'TSQL',                                               "
    " @cmdexec_success_code = 0,                                         "
    " @flags = 4,                                                         "
    " @retry_attempts = 0,                                                "
    " @retry_interval = 0,                                                "
    " @output_file_name = N'',                                            "
    " @on_success_step_id = 0,                                            "
    " @on_success_action = 1,                                              "
    " @on_fail_step_id = 0,                                                 "
    " @on_fail_action = 2                                                  "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                         "
    " BEGIN                                                                      "
    " PRINT '不能建立完全备份的作业步骤'                                    "
    " RETURN                                                               "
    " END                                                                        "

    " EXECUTE @iResult = sp_update_job @job_id = @JobID1, @start_step_id = 1     "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                      "
    " BEGIN                                                             "
    "  PRINT '不能添加完全备份的作业步骤'                          "
    " RETURN                                                      "
    " END                                                               "

    " EXECUTE @iResult = sp_add_jobschedule @job_id = @JobID1,          "
    " @name = N'第 1 调度',                                      "
    " @enabled = 1, @freq_type = 4,                              "
    " @active_start_date = 20060501,                              "
    " @active_start_time = 0000, @freq_interval = 1,             "
    " @freq_subday_type = 8, @freq_subday_interval =1,           "
    " @freq_relative_interval = 0, @freq_recurrence_factor = 0,   "
    " @active_end_date = 99991231, @active_end_time = 235959      "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                "
    " BEGIN                                                             "
    " PRINT '不能添加完全备份的作业调度'                         "
    " RETURN  "
    " END           "

    " EXECUTE @iResult = sp_add_jobserver @job_id = @JobID1, @server_name = N'(local)'                             "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                                           "
    " BEGIN                                                                                                        "
    " PRINT '不能添加完全备份的目标服务器'                                                                   "
    " RETURN                                                                                                 "
    " END                                                                                                          "

    " EXECUTE @iResult = sp_add_maintenance_plan_job @PlanID, @JobID1                                             "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                                          "
    " BEGIN                                                                                                       "
    " PRINT '不能把完全备份的作业关联到维护计划'                                                            "
    " RETURN                                                                                                 "
    " END                                                                                                          "

    " SET @JobName = @PlanName + N'的事务日志备份作业'                                                            "
    " SET @JobCommand = N'EXECUTE master.dbo.xp_sqlmaint N''-PlanID ' + CONVERT(VARCHAR(40), @PlanID) +           "
    " ' -VrfyBackup -BkUpMedia '+@path+' -BkUpLog  -UseDefDir  -DelBkUps 4DAYS -CrBkSubDir -BkExt '+@back_2+'''' "

    " EXECUTE @iResult = sp_add_job @job_id = @JobID2 OUTPUT ,                           "
    " @job_name = @JobName,                                                        "
    " @owner_login_name = N'sa',                                                   "
    " @description = N'No description available.',                                 "
    " @category_name = NULL,                                                       "
    " @enabled = 1,                                                                "
    " @notify_level_email = 0,                                                     "
    " @notify_level_page = 0,                                                      "
    " @notify_level_netsend = 0,                                                   "
    " @notify_level_eventlog = 2,                                                  "
    " @delete_level= 0                                                              "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                 "
    " BEGIN                                                                              "
    " PRINT '不能建立事务日志备份的作业'                                           "
    "  RETURN                                                                       "
    " END                                                                                "
    " EXECUTE @iResult = sp_add_jobstep @job_id = @JobID2,                               "
    " @step_id = 1,                                                                "
    " @step_name = N'第 1 步',                                                     "
    " @command = @JobCommand,                                                      "
    " @database_name = N'master',                                                  "
    " @server = N'',                                                               "
    " @database_user_name = N'',                                                   "
    " @subsystem = N'TSQL',                                                        "
    " @cmdexec_success_code = 0,                                                   "
    " @flags = 4,                                                                  "
    " @retry_attempts = 0,                                                         "
    " @retry_interval = 0,                                                         "
    " @output_file_name = N'',                                                     "
    " @on_success_step_id = 0,                                                     "
    " @on_success_action = 1,                                                      "
    " @on_fail_step_id = 0,                                                        "
    " @on_fail_action = 2                                                          "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                 "
    " BEGIN                                                                              "
    " PRINT '不能建立事务日志备份的作业步骤'                                        "
    " RETURN                                                                        "
    " END                                                                                "

    " EXECUTE @iResult = sp_update_job @job_id = @JobID2, @start_step_id = 1              "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                   "
    " BEGIN                                                                                "
    " PRINT '不能添加事务日志备份的作业步骤'                                          "
    " RETURN                                                                          "
    " END                                                                                   "

    " EXECUTE @iResult = sp_add_jobschedule @job_id = @JobID2,                              "
    " @name = N'第 1 调度', @enabled = 1, @freq_type = 4,                             "
    " @active_start_date = 20000101, @active_start_time = 0000,                       "
    " @freq_interval = 1, @freq_subday_type = 8,                                       "
    " @freq_subday_interval = 1, @freq_relative_interval = 0,                          "
    " @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959 "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                                                  "
    " BEGIN                                                                              "
    " PRINT '不能添加事务日志备份的作业调度'                                       "
    " RETURN                                                                       "
    " END                                                                               "

    " EXECUTE @iResult = sp_add_jobserver @job_id = @JobID2, @server_name = N'(local)'   "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                               "
    " BEGIN                                                           "
    " PRINT '不能添加事务日志备份的目标服务器'                  "
    " RETURN                                                   "
    " END                                                            "
                                                                   
    " EXECUTE @iResult = sp_add_maintenance_plan_job @PlanID, @JobID2  "
    " IF (@@ERROR <> 0 OR @iResult <> 0)                      "
    " BEGIN                                                   "
    " PRINT '不能把事务日志备份的作业关联到维护计划'   "
    " RETURN                            "
    " END " ;
        ADO_com_execsql->CommandText=sql;
                     try{
                           ADO_com_execsql->Execute();
                           return true;
                        }
                     catch(...)
                     {
                       ShowMessage("创建错误");
                       return false;
                     }

  • 相关阅读:
    C语言作业(心理魔术)
    心理魔术
    作业
    作业
    自定义打包小游戏的build template,接入SDK,
    JavaScript_call,bind,apply的区别
    JavaScript原型链的理解
    学习笔记—前端基础之ES6的数组
    学习笔记 — 前端基础之ES6(2)
    学习笔记 — 前端基础之ES6(1)
  • 原文地址:https://www.cnblogs.com/jamin/p/1324172.html
Copyright © 2020-2023  润新知