如何:更改目标平台并发布数据库项目How to: Change Target Platform and Publish a Database Project

可以将 SQL Server Data Tools (SSDT) 数据库项目的目标 SQL Server 版本更改为任何受支持的 SQL Server 实例(SQL Server 2005、2008、2008 R2、Microsoft SQL Server 2012 或 SQL Azure)。You can change the target SQL Server version for your SQL Server Data Tools (SSDT) database project to any supported instance of SQL Server (SQL Server 2005, 2008, 2008 R2, Microsoft SQL Server 2012, or SQL Azure). 通过这样做,可以针对一个项目进行数据库开发,但在需要时将其发布到多种不同风格的 SQL Server 实例中。By doing so, you can centralize your database development in one project, but publish it to multiple SQL Server instances as the need arises.

SSDT 还通过识别您的目标平台并且自动检测出您的代码中的任何错误(例如,在您为将要发布到 SQL Azure 的项目使用不支持的功能时),对此任务进行简化。SSDT also makes this task simple by being aware of your target platform and automatically detecting any error in your code (for example., when you are using unsupported features for a project that is going to be published to SQL Azure).


以下过程利用在连接的数据库开发面向项目的脱机数据库开发部分中的之前过程中创建的实体。The following procedures utilize entities created in previous procedures in the Connected Database Development and Project-Oriented Offline Database Development sections.

更改项目的目标平台To change a project's target platform

  1. 在“解决方案资源管理器”中右键单击你的项目并选择“属性”。Right-click your project in Solution Explorer and select Properties. 单击左侧的 “项目设置” 选项卡以便访问 “项目设置” 属性页。Click the Project Settings tab on the left to access the Project Settings property page.

  2. 此页中的 “目标平台” 下拉列表包含某一数据库项目可发布到的所有支持的 SQL Server 平台。The Target platform dropdown list in this page contains all the supported SQL Server platforms that a database project can be published to. 为此过程,请选择 SQL AzureFor this procedure, select SQL Azure.

在编辑脚本时使用平台验证To use platform validation when editing scripts

  1. 在“解决方案资源管理器”中右键单击 Products 表,然后选择“查看代码”以便在 Transact-SQL 编辑器中打开它。Right-click the Products table in Solution Explorer, and select View Code to open it in Transact-SQL Editor.

  2. ON [PRIMARY] 追加到 CREATE TABLE 语句的末尾。Append ON [PRIMARY] to the end of the CREATE TABLE statement.

  3. 请注意,“错误列表”窗格中会显示以下错误:SQL70015:SQL Azure 中不支持“文件组引用和分区方案”。Notice that the following error shows up in the Error List pane: SQL70015: 'Filegroup reference and partitioning scheme' is not supported in SQL Azure..

    SSDT 将基于目标平台自动验证您的脚本。SSDT automatically validates your script based on the target platform. 在这种情况下,因为 SQL Azure 不支持文件组,所以 SSDT 将返回错误。In this case, since filegroup is not supported in SQL Azure, SSDT returns an error. 有关 SQL Azure 中不支持的 Transact-SQL 语句,请参阅部分支持的 Transact-SQL 语句(Microsoft Azure SQL 数据库)For a list of non-supported Transact-SQL statements in SQL Azure, see Partially Supported Transact-SQL Statements (Microsoft Azure SQL Database).

  4. 删除 ON 子句。Remove the ON clause. 请注意,该错误将立即从 “错误列表” 中消失。Notice that the error immediately disappears from the Error List.

发布数据库项目To publish a database project

  1. 如果您有权访问 SQL Azure 实例,则可以跳到下一步。If you have access to a SQL Azure instance, you can skip to the next step. 否则,在“解决方案资源管理器”中右键单击 TradeDev 项目,然后选择“属性”以便访问“项目设置”属性页。Otherwise, right-click the TradeDev project in Solution Explorer and select Properties to access the Project Settings property page. 使用“目标平台”下拉列表选择想要将项目发布到的 SQL Server 平台。Use the Target platform dropdown list to select the SQL Server platform that you want to publish the project to.

  2. 在“解决方案资源管理器”中右键单击 TradeDev 项目,然后选择“发布”。Right-click the TradeDev project in Solution Explorer and select Publish. SSDT 将开始生成您的项目。SSDT will start building your project. 如果没有生成错误, “发布数据库” 对话框将出现。If there is no build error, the Publish Database dialog box appears.

  3. “发布数据库” 对话框中,单击 “编辑” 以编辑目标数据库连接。In the Publish Database dialog box, click Edit to edit the Target database connection.

  4. 在“连接属性”对话框中,输入你的 SQL Server 实例名称和用于身份验证的凭据。In the Connection Properties dialog box, enter your SQL Server instance name and your credentials for authentication. “连接到数据库” 中,输入 NewTradeIn Connect to a database, enter NewTrade. 这将尝试将您的数据库项目发布到新数据库。This will attempt to publish your database project to a new database. 您也可以选择要发布到的现有数据库。You can also choose an existing database to publish to. 例如,如果选择现有的 TradeDev 数据库,然后在脱机 TradeDev 项目中对这些对象已在进行的所有更改(作为脚本)将传播到实时 TradeDev 数据库中。For example, if you choose the existing TradeDev database, then all the changes you have been making to the objects (as scripts) in the offline TradeDev project will be propagated to the live TradeDev database.

    如果您有权对想要发布到的数据库进行任何更改,则按下 “发布” 按钮。If you have permission to make any changes to the database you want to publish to, press the Publish button. 但是,如果对生产数据库没有写访问权限,则可以单击“生成脚本”按钮以便生成一个 Transact-SQL 发布脚本,然后可以将该脚本交给某一 DBA。If, however, you do not have write access to a production database, you can click the Generate Script button to produce a Transact-SQL publish script, which can then be handed off to a DBA. 然后,该 DBA 可以运行该脚本以更新生产服务器,以便其架构与数据库项目同步。The DBA can then run the script to update the production server so that its schema is in sync with the database project.

  5. “数据工具操作” 窗口将显示发布操作的进度,并在出现任何错误时通知你。The Data Tools Operations window will show the progress of your publish operations, and notify you of any errors. 在此新窗口中,您还可以根据需要选择查看部署预览、生成的脚本或完整发布结果。In this new window, you can also choose to view the deployment preview, the generated script, or the full publish results if desired.

  6. 您还可以将发布设置保存到某一配置文件中,以便为将来的发布操作重复使用相同的设置。You can also save the publish settings in a profile, so that you can reuse the same settings for future publish operations. 为此,在 “发布数据库” 对话框中单击 “将配置文件另存为” 按钮。To do so, click the Save Profile As button in the Publish Database dialog box. 在将来,您可以在想要重新加载现有设置时单击 “加载配置文件” 按钮。In the future, you can click the Load Profile button when you want to reload existing settings.

  7. 请注意 “数据工具操作” 窗口中的消息。Notice the messages in the Data Tools Operations window. 单击“创建发布预览…”右侧的“查看预览”链接右侧的“视图预览”链接。这将打开部署预览报表。Click on "View Preview" link to the right of Creating publish preview... This will open the deployment preview report. 如果项目的目标平台并不是项目所发布到的数据库服务器,则 SSDT 将在报表中发出警告。If your project's target platform is not identical to the database server where the project is published to, SSDT will issue a warning in this report. 例如,如果项目的目标平台是 Microsoft SQL Server 2012 并且正在尝试将该项目发布到某一 SQL Server 2008 R2 服务器实例,则你将会在“输出”窗口中看到以下警告:For example, if your project's target platform is Microsoft SQL Server 2012 and you are attempting to publish the project to a SQL Server 2008 R2 server instance, you will see the following warning in the Output window:

将 Microsoft SQL Server 2012 指定为目标平台的项目可能会遇到与 SQL Server 2008 的兼容性问题 如果此类项目包含 Microsoft SQL Server 2012 中引入的实体(例如,一个 Sequence 对象),则发布操作将失败。A project which specifies Microsoft SQL Server 2012 as the target platform may experience compatibility issues with SQL Server 2008 If such project contains entities (for example, a Sequence object) that are introduced in Microsoft SQL Server 2012, the publishing operation will fail.

The deployment will fail if object predicates use **CONTAINS** or **FREETEXT** over a newly created full-text index and transactional scripts are used. If the option to include transactional scripts is enabled during deployment, then procedures and views are defined inside a transaction while a full-text index is defined outside of a transaction at the end of the deploy script. Because of this ordering in the script, procedures or views using CONTAINS or FREETEXT will not be resolved against the full-text index, resulting in a deployment error.