编写SQL Server数据库对象脚本的方法

本文详细介绍了在SQL Server中为数据库对象编写脚本的两种主要方法:使用'生成脚本向导'和操作DACPAC。通过向导,你可以自定义脚本选项,包括对象选择和版本配置。DACPAC则允许在不考虑数据的情况下导出和导入数据库模式。
摘要由CSDN通过智能技术生成

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数据库,然后启动“生成脚本”向导,如下所示。

Generate Scripts Wizard

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

    生成脚本并保存

Generate Scripts Wizard for database objects

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

    全文目录

Select objects

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

    脚本到剪贴板

Advanced configurations

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

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

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 Drop and Create

  • 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 Server version

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

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

    Script for the database engine 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,以便它也生成那些脚本

    Script logins, Object-level permissions, Owner, Statistics

  • 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

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

    Type of data to the script

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

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

    Table/ View Options

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

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

Review your 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.

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

Preparing scripts

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中的所需数据库,您可以看到“数据层应用程序”选项。

Data-tier application 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文件,我们可以选择此选项来覆盖现有文件

DACPAC export and Import

On the next page, validate the configurations.

在下一页上,验证配置。

Validation & Summary

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

builds a DACPAC package

单击下一步,并为源数据库构建一个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数据库节点,然后单击部署数据层应用程序

Deploy data-tier applications

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

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

high-level steps for DACPAC import
  • 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包。

select the DAC package

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.

Deployed DAC and database

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

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

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

Specify a new database name

Review the deployment configuration.

查看部署配置。

Review the deployment configuration

Click Next, and it starts the DACPAC deployment.

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

DACPAC deployment

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

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

refresh SQL databases

结论 (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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值