在迁移或升级SQL Server数据库服务器时,很多场景下我们不能还原msdb,所以我们必须手工迁移SQL Server相关作业。如果手工生成每一个作业的脚本话,费时又费力,其实SQL Server中有好几种方法可以批量生成作业脚本的方法, 下面介绍一下。

1:SSMS客户端工具批量生成创建作业脚本

1:在SSMS—>SQL Server Agent下,单击Jobs。

2: 按快捷键F7,在Object Explorer Details里面,你会看到所有的作业

3: 全选所有作业(CTRL+A),然后右键单击“Script Job as”,然后选项“CREATE TO"就会生成所有作业的脚本。

这个方法其实是非常简洁方便的。不足之处就是所有作业的脚本位于同一个Script,没有按作业名生成相关对应的脚本。

2:使用PowerShell脚本生成所有作业的脚本

有一个现成的PowerShell脚本,相关介绍以及脚本下载具体参考下面链接:

https://gallery.technet.microsoft.com/scriptcenter/How-to-get-the-of-all-the-81859696

PS C:\Users> M:\GetJobScripts\GetAllAgentJobs.ps1

cmdlet GetAllAgentJobs.ps1 at command pipeline position 1

Supply values for the following parameters:

ServerName: YourServerName

FilePath: M:\GetJobScripts

Scripting out  xxxxxxxxxxxx  successfully!

Scripting out  xxxxxxxxxxxx  successfully!

.........................................

使用该PowerShell脚本可以生成各个作业的各自脚本。非常简洁方便。不过该脚本要求Windows PowerShell 2.0或更高的版本,另外,有些平台是没有测试过的,需要参考上面链接说明。 GetAllAgentJobs.ps1的脚本如下:

<#

 The sample scripts are not supported under any Microsoft standard support 

 program or service. The sample scripts are provided AS IS without warranty  

 of any kind. Microsoft further disclaims all implied warranties including,  

 without limitation, any implied warranties of merchantability or of fitness for 

 a particular purpose. The entire risk arising out of the use or performance of  

 the sample scripts and documentation remains with you. In no event shall 

 Microsoft, its authors, or anyone else involved in the creation, production, or 

 delivery of the scripts be liable for any damages whatsoever (including, 

 without limitation, damages for loss of business profits, business interruption, 

 loss of business information, or other pecuniary loss) arising out of the use 

 of or inability to use the sample scripts or documentation, even if Microsoft 

 has been advised of the possibility of such damages. 

#>

 

Param(

[Parameter(Mandatory = $true, position = 0)][string] $ServerName,

[Parameter(Mandatory = $true, position = 1)][string] $FilePath

)

if(Test-Path $FilePath)

{

        #check if the instance name is available on the server

        

         [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |Out-Null

         $srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName 

         if($ServerName.contains($env:COMPUTERNAME) -and ($srv.VersionString))

         {

            $jobs = $srv.JobServer.Jobs | Where-Object {$_.category -notlike "*repl*" -and $_.category -notlike "*shipping*" -and $_.category -notlike "*Maintenance*" }  

 

            ForEach ( $job in $jobs) 

            { 

               $jobname = $FilePath +'\' + $job.Name.replace(" ","_").replace("\","_").replace("[","_").replace("]","_").replace(".","_").replace(":","_").replace("*","_") + ".sql" 

              $job.Script() | Out-File $jobname 

                Write-Host 'Scripting out ' $job ' successfully!'

            }

         }

        else 

        {

        Write-Host 'The server name you entered is not available!'

        }

 

}

else

{

Write-Host 'The path does not exist, please retype again!'

}

方法3:通过SqlDmo组件相关函数生成数据库对象脚本,不过有些版本由于没有安装"Backward Compatibility Components" 导致该脚本不能创建相关脚本文件,个人测试时也遇到不少问题,有兴趣可以试试。

http://www.databasejournal.com/features/mssql/article.php/2205291/Generate-Scripts-for-SQL-Server-Objects.htm

参考资料:

https://stackoverflow.com/questions/3361163/automatically-create-scripts-for-all-sql-server-jobs

https://gallery.technet.microsoft.com/scriptcenter/How-to-get-the-of-all-the-81859696

http://www.databasejournal.com/features/mssql/article.php/2205291/Generate-Scripts-for-SQL-Server-Objects.htm

MS SQL批量生成作业脚本方法介绍总结的更多相关文章

  1. jinja2批量生成python脚本

    ​ 在使用airflow的过程中需要大量的dag脚本进行性能测试,如果一个个去编写dag脚本未免太过麻烦,于是想到用python的jinja2模板引擎实现批量脚本生成. 先通过pip命令安装jinja ...

  2. MS SQL SERVER 数据库日志压缩方法与代码

    MS SQL性能是很不错的,但是数据库用了一段时间之后,数据库却变得很大,实际的数据量不大.一般都是数据库日志引起的!数据库日志的增长可以达到好几百M. DUMP TRANSACTION [数据库名] ...

  3. PS如何批量生成缩略图(方法可以通用其他重复劳动)

    原图 缩略 进入正题,学生时代玩过脚本精灵的应该一点就通 原理就是:录制一系列动作,然后生成脚本,并执行(经常PS水印代码的一个可以用程序实现,一个就可以用PS脚本) 新建一个组 新建一个动作 进行你 ...

  4. 批量生成DDL脚本

    获取用户下所有索引脚本,用于数据迁移后重建索引: set pagesize 0set long 90000set feedback offset echo offspool get_index_ddl ...

  5. Swift - 下标脚本方法介绍及实例

    定义下标脚本之后,可以使用“[]”来存取数据类型的值. 示例1:实现一个我们自定的字符串类,可以方便的通过索引获取某一个字符值,或某一部分字符串.同时也可以通过索引,给某一部分赋值. 1 2 3 4 ...

  6. MS SQL SERVER执行大脚本文件时,提示“内存不足”的解决办法

    问题描述: 当客户服务器不允许直接备份时,往往通过导出数据库脚本的方式来部署-还原数据库, 但是当数据库导出脚本很大,用Microsoft SQL Server Management Studio执行 ...

  7. 批量生成clr脚本

    use [dbname]go --1.产生crl程序集的sql --定义表变量,临时存储中间结果集declare @tb table(name nvarchar(100),permission_set ...

  8. MS SQL 批量给存储过程/函数授权

    在工作当中遇到一个类似这样的问题:要对数据库账户的权限进行清理.设置,其中有一个用户Test,只能拥有数据库MyAssistant的DML(更新.插入.删除等)操作权限,另外拥有执行数据库存储过程.函 ...

  9. [SQL]批量 更改字符集脚本,批量查询约束,批量查询索引

    How to change collation of all database objects in SQL Server. Have you encountered a problem where ...

随机推荐

  1. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  2. 《JavaScript总结》apply、call和bind方法

    在JavaScript中,apply.call.bind这个三个方法,它们的作用都是为了改变某个函数运行时的上下文, 也就是改变函数体内的this指向. 在一个函数里,存在“定义时上下文”.“运行时上 ...

  3. 基于python的图片修复程序-可用于水印去除

    图片修复程序-可用于水印去除 在现实的生活中,我们可能会遇到一些美好的或是珍贵的图片被噪声干扰,比如旧照片的折痕,比如镜头上的灰尘或污渍,更或者是某些我们想为我所用但有讨厌水印,那么有没有一种办法可以 ...

  4. JQ-bootstrap我的开源前端框架

        因为实在不知道写啥,所以迟迟没有相关的介绍.但是必须要积累过程资产,所以还是介绍一下,不定哪天就有人用了.       首先还是介绍遇到的问题,我是做传统后台管理系统的,公司赶时髦,要用boo ...

  5. SpringMVC学习(三)———— springmvc的数据校验的实现

    一.什么是数据校验? 这个比较好理解,就是用来验证客户输入的数据是否合法,比如客户登录时,用户名不能为空,或者不能超出指定长度等要求,这就叫做数据校验. 数据校验分为客户端校验和服务端校验 客户端校验 ...

  6. Jenkins自动化构建配置

    写在前头 我每次修改代码一点东西,都要进行一个重新发布.重新发布的流程大概如下: 将最新代码同步上传到git上面 Maven打包 mvn clean install 将最新的jar包上传到服务器上面, ...

  7. [JavaScript] 前端模块加载简单实现(require)

    模块加载的简单实现 (function(win) { var baseUrl; var paths; var script_cache = {}; var script_queue = []; var ...

  8. Js中的提升

    Js的执行并不是由上向下一行一行顺序下来的,一个例子如下: a = 2; var a ; console.log(a) 输出的是2,这区别于别的语言. 再一个例子: console.log(a); / ...

  9. RNN入门(4)利用LSTM实现整数加法运算

      本文将介绍LSTM模型在实现整数加法方面的应用.   我们以0-255之间的整数加法为例,生成的结果在0到510之间.为了能利用深度学习模型模拟整数的加法运算,我们需要将输入的两个加数和输出的结果 ...

  10. js如何获取url参数

    匹配URL参数的正则是: var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", ...