In this article, we will explore various ways for scripting SQL Server database objects.

在本文中,我们将探索编写SQL Server数据库对象脚本的各种方法。

介绍 (Introduction)

DBAs and developers manage environments for a SQL database. While managing multiple copies, sometimes we require the following tasks.

DBA和开发人员管理SQL数据库的环境。 在管理多个副本时,有时我们需要执行以下任务。

  1. Database refresh from a production environment to the lower environments. It requires copying all database objects such as database schema, tables, stored procedures, functions, indexes. We use multiple approaches such as database backup restore, object scripts, BACPAC method depending upon the requirement 数据库从生产环境刷新到较低环境。 它需要复制所有数据库对象,例如数据库架构,表,存储过程,函数,索引。 我们根据需要使用多种方法,例如数据库备份还原,对象脚本,BACPAC方法
  2. Sometimes, we do not require data and have the requirement to export and import all objects in a destination database 有时,我们不需要数据,而是需要导出和导入目标数据库中的所有对象

In this article, we will explore different ways to export database objects without data.

在本文中,我们将探索不使用数据导出数据库对象的不同方法。

为SQL数据库生成脚本向导 (Generate Scripts Wizard for SQL Database)

We can use Generate Scripts wizard for scripts of all database objects or specific objects. It provides various configuration options to choose from.

我们可以使用“ 生成脚本”向导生成所有数据库对象或特定对象的脚本。 它提供了多种配置选项供您选择。

Right-click on the source SQL database for which you want to generate script and launch Generate scripts wizard as shown below.

右键单击要为其生成脚本的源SQL数据库,然后启动“生成脚本”向导,如下所示。

It launches generate scripts wizard with a brief introduction and high-level steps.

它启动带有简要介绍和高级步骤的生成脚本向导。

  • Select database objects 选择数据库对象
  • Specify scripting options 指定脚本选项
  • Review your selections 查看您的选择
  • Generate script and save them 生成脚本并保存

In the next step, we get options to script an entire database or select specific objects from the following list.

在下一步中,我们将获得用于编写整个数据库脚本或从以下列表中选择特定对象的选项。

  • Tables 桌子
  • Views 观看次数
  • Stored procedure 存储过程
  • User-defined functions 用户定义的功能
  • User-defined data types 用户定义的数据类型
  • DDL triggers DDL触发器
  • XML schema collections XML模式集合
  • Schemas 模式
  • Full-text catalogs 全文目录

Let’s select the first option to script the entire database and click Next. Here, you get different options to save the script.

让我们选择第一个选项来编写整个数据库的脚本,然后单击“下一步”。 在这里,您将获得不同的选项来保存脚本。

  • Script to file 脚本文件
  • Script to a new query window 脚本到新的查询窗口
  • Script to clipboard 脚本到剪贴板

We can still do a lot with this generate script wizard. Click on Advanced. It opens Advanced Scripting Options.

使用此生成脚本向导,我们仍然可以做很多事情。 单击高级 。 它会打开“ 高级脚本选项”。

In this window, you can do the required changes for generating a script for the selected objects. We cannot cover all configurations here. However, I will highlight a few useful changes. Few configurations are true, by default.

在此窗口中,您可以进行必要的更改以为所选对象生成脚本。 我们无法在此处介绍所有配置。 但是,我将重点介绍一些有用的更改。 默认情况下,很少有配置是正确的。

  • Script Drop and Create: Default configuration is to generate script with a Create statement. We can change it to Script Drop, Script Drop and create as well

    脚本删除和创建:默认配置是使用Create语句生成脚本。 我们可以将其更改为Script Drop,Script Drop并创建

  • Script for Server version: We might require a script for a different version of SQL Server. We can use SQL Server versions starting from SQL Server 2005. You should generate a script for an appropriate version as few commands and syntaxes do not work in a different version of SQL Server

    服务器版本的脚本我们可能需要用于其他版本SQL Server的脚本。 我们可以使用从SQL Server 2005开始SQL Server版本。您应该为适当的版本生成脚本,因为很少有命令和语法在其他版本SQL Server中不起作用

  • Script for the database engine edition: We can choose the required database engine edition, and it scripts features compatible with that edition

    数据库引擎版本的脚本:我们可以选择所需的数据库引擎版本,并且该脚本编写与该版本兼容的功能

  • Script logins, Object-level permissions, Owner, Statistics: By default, it does not script any of the logins, object-level permissions, database owner and statistics. We can change the corresponding value to true so that it generates those scripts as well

    脚本登录名,对象级权限,所有者,统计信息:默认情况下,它不编写任何登录名,对象级权限,数据库所有者和统计信息的脚本。 我们可以将相应的值更改为true,以便它也生成那些脚本

  • Type of data to the script: By default, it generates a script for the selected objects. It does not script data. We can modify this configuration and choose from Data only, Schema and data and schema only values

    脚本的数据类型:默认情况下,它将为所选对象生成脚本。 它不编写数据脚本。 我们可以修改此配置,并从“仅数据”,“模式”和“数据和仅模式”值中选择

  • Table/ View Options: it provides various configurations for script tables and views such as script constraints, indexes, primary and foreign keys

    表/视图选项:它为脚本表和视图提供各种配置,例如脚本约束,索引,主键和外键

Once you have configured the required options, click next for review of the selections.

配置必需的选项后,单击“下一步”以查看选择。

You can go back and change any settings. If no changes required, click Next and you can see the status for each object script. Once you finish it, you can see the script per the configured option.

您可以返回并更改任何设置。 如果不需要更改,请单击“ 下一步” ,您可以查看每个对象脚本的状态。 完成后,您可以按照配置的选项查看脚本。

DACPAC进出口 (DACPAC export and Import)

In the article Importing a BACPAC file for a SQL database using SSMS, we explored the use of a BACPAC package file for export tables data and import them into a new database. Later, we use it for data refresh from the source to the destination database.

在使用SSMS为SQL数据库导入BACPAC文件的文章中,我们探讨了BACPAC包文件用于导出表数据并将其导入到新数据库中的方法。 稍后,我们将其用于从源到目标数据库的数据刷新。

Data-Tier Application Package (DACPAC) creates a logical package consisting schema of database objects. We can exchange these packages between developers, DBAs for exporting database schema without worrying about the data. We can use SQL Server Data tools and SQL Server Management Studio for this.

d ATA-层中 PPLIÇ通货膨胀的Pac卡格(DACPAC)创建由数据库对象的模式的逻辑包。 我们可以在开发人员,DBA之间交换这些软件包,以导出数据库架构而不必担心数据。 为此,我们可以使用SQL Server数据工具和SQL Server Management Studio。

Right-click on the desired database in SSMS and you can see Data-tier application options.

右键单击SSMS中的所需数据库,您可以看到“数据层应用程序”选项。

In the same previous article (link mentioned above), we explored the Export data-tier application for a BACPAC package. We use the Extract Data-tier application for a DACPAC package export.

在上一篇相同的文章(上面提到的链接)中,我们探讨了BACPAC软件包的Export data-tier应用程序。 我们将提取数据层应用程序用于DACPAC包导出。

First, it launches an introduction page specifying high-level steps.

首先,它启动一个介绍页面,指定高级步骤。

  • Set the DAC properties 设置DAC属性
  • Review object dependencies and validation results 查看对象依赖性和验证结果
  • Build the DAC package 构建DAC包

On the next page, specify the following values.

在下一页上,指定以下值。

  • Application name 应用名称
  • Version: We can create different versions of a DACPAC package. By default, it uses first DACPAC version 1.0.0.0 版本 :我们可以创建DACPAC软件包的不同版本。 默认情况下,它使用第一个DACPAC版本1.0.0.0
  • Description: It is an optional field. It is always good to give a short description specifying the purpose of this export. We can skip the description as of now 说明 :这是一个可选字段。 简短说明此导出的目的总是很有益的。 到目前为止,我们可以跳过描述
  • Save to DACPAC package file: Specify the directory for storing this package file. Y default, it stores the file inside the SSMS directory 保存到DACPAC软件包文件:指定用于存储此软件包文件的目录。 是默认设置,它将文件存储在SSMS目录中
  • Overwrite existing file: If the DACPAC file with the same name already exists, we can choose this option to overwrite an existing file 覆盖现有文件:如果已经存在同名的DACPAC文件,我们可以选择此选项来覆盖现有文件

On the next page, validate the configurations.

在下一页上,验证配置。

Click next, and it builds a DACPAC package for the source database

单击下一步,并为源数据库构建一个DACPAC程序包

部署数据层应用程序 (Deploy data-tier applications)

Once we have DACPAC export, we can import it in the desired instance as a new database. Right-click on SQL database node and click on Deploy data-tier applications.

导出DACPAC后,可以在所需实例中将其导入为新数据库。 右键单击SQL数据库节点,然后单击部署数据层应用程序

It launches the following wizard along with the introduction and high-level steps for DACPAC import.

它启动以下向导以及DACPAC导入的简介和高级步骤。

  • Select the DAC package 选择DAC封装
  • Configure the deployment 配置部署
  • Review the selection 查看选择
  • Deploy a DAC package 部署DAC程序包

In the next step, select the DAC package we exported earlier.

在下一步中,选择我们之前导出的DAC包。

Click Next and specify the database name. By default, it takes the source database name. In the following screenshot, you can see a red dot that shows that this database [AdventureWorks] already exists in this instance.

单击下一步,然后指定数据库名称。 默认情况下,它采用源数据库名称。 在下面的屏幕快照中,您可以看到一个红点,表明该数据库[AdventureWorks]已存在于此实例中。

Specify a new SQL database name, and a red dot icon disappears.

指定一个新SQL数据库名称,一个红点图标消失。

Review the deployment configuration.

查看部署配置。

Click Next, and it starts the DACPAC deployment.

单击“ 下一步” ,它开始DACPAC部署。

Once the deployment is finished, refresh databases in the Object Explorer in SSMS.

部署完成后,在SSMS的对象资源管理器中刷新数据库。

结论 (Conclusion)

In this article, we explored Generate Scripts wizard and DACPAC package for export/import schema for SQL database objects. You should review the approaches and use the best fit for you.

在本文中,我们探索了用于SQL数据库对象导出/导入模式的“生成脚本”向导和DACPAC包。 您应该查看方法并为您使用最合适的方法。

翻译自: https://www.sqlshack.com/methods-to-script-sql-server-database-objects/

编写SQL Server数据库对象脚本的方法相关推荐

  1. 如何记录SQL Server数据库对象

    介绍 (Introduction) In any good programming reference, you will read that a developer has to document ...

  2. SQL Server数据库收缩日志的方法

    SQL Server数据库收缩日志的方法: 方法一 一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大. 1.设置数据库模式为 ...

  3. SQL Server 数据库对象

    SQL Server数据库对象 SQL Server的数据库一共有11种对象,包括关系图.表.视图.存储过程.用户.角色.规则.默认.用户定义的数据类型.用户定义的函数.全文目录. 关于这11种数据库 ...

  4. asp教程连接sql server数据库教程二种方法

    asp教程连接sql server数据库教程二种方法  ,sql server ole db连接方法:,本人链接sql2012已经成功,想可以帮助大家 本教程主要是讲了asp与mssql server ...

  5. SQL Server数据库六种数据移动方法

    1. 通过工具DTS的设计器进行导入或导出DTS的设计器功能强大,支持多任务,也是可视化界面,容易操作,但知道的人一般不多,如果只是进行SQL Server数据库中部分表的移动,用这种方法最好,当然, ...

  6. SQL Server 数据库清除日志的方法

    方法一: 1.打开查询分析器,输入命令  BACKUP LOG database_name WITH NO_LOG  2.再打开企业管理器--右键要压缩的数据库--所有任务--收缩数据库--收缩文件- ...

  7. SQL Server 数据库巡检脚本

    --1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb --3.查看数据库所在机器的操作系统参数 exec master..xp_ ...

  8. 移动Sql Server数据库的脚本

    前段时间做过这么一件事情,把原本放在c盘的所有数据库(除了sql server系统文件外)文件Move到D盘,主要是为了方便后续管理以及减少磁盘I/O阻塞(C,D是2个独立磁盘). 脚本很简单,在此只 ...

  9. windows服务器系统巡检脚本,sql server 数据库巡检脚本

    我司数据库一季度巡检一次,所以经常用到下面脚本 --1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb --3.查看数据库所在机器 ...

最新文章

  1. Go 知识点(18)— 条件编译(编译标签、文件后缀)
  2. KDE与GNOME的起源与发展
  3. SO_REUSEADDR和SO_REUSEPORT
  4. 【UVA11324】The Largest Clique (SCC)
  5. 《Excel最强功能-数据透视表》 网课笔记
  6. Oracle 左连接、右连接、全外连接、(+)号作用
  7. SiteServer CMS 新版本 V6.13(2019年11月1日发布)
  8. 用Java对HashMap排序
  9. arcgis不闭合线转面_【干货】ArcGIS矢量面转点、点转面,面矢量转点矢量方法讲解,值得学习!...
  10. 力扣有没有java_【Java】一篇文章带你玩转用Java刷力扣
  11. oracle 约束 年份,如何在Oracle中确定给定年份是否为Le年?
  12. python创建文件的编码格式
  13. 高中电子技术——指针式万用表调零
  14. jumpserver跳板机docker安装小小趟坑
  15. 深入浅出MySQL之优化
  16. 计算机与不确定性原理,傅里叶变换和不确定性原理
  17. linux使用TCP聊天室设计详解,基于linux的TCP网络聊天室设计与实现
  18. Qcon 实时音视频专场:实时互动的最佳实践与未来展望
  19. 华为 eNSP 模拟器配置动态路由(ospf)
  20. 服务器硬盘数据备份到nas,谁说固态硬盘做存储是鸡肋?NAS存储服务器还能这样玩...

热门文章

  1. 五、spring-data-Jpa 数据库操作
  2. systemd的作用
  3. 【原创】Elasticsearch无宕机迁移节点
  4. Linux内核分析——进程的描述和进程的创建
  5. 8皇后问题--回溯法 (循环递归)
  6. 当网站不允许上传ASP,CGI,CER等脚本文件时
  7. SWFUpload多文件上传,文件大小增大问题
  8. JS:ES6-7 迭代器与生成器
  9. 牛客网模拟笔试——膨胀的牛牛(JavaScript)
  10. JavaScript数据结构——树(Tree)