在配置新的SQL Server 2019时设置兼容性级别。在运行管道时,可以设置此功能吗?

发布于 2025-02-13 21:07:22 字数 138 浏览 0 评论 0 原文

我们最近通过提供新的VM迁移到SQL Server 2019。我们想将向后兼容性设置为真实,因为我们的旧服务器在SQL Server 2012中。

在SQL Server安装过程中运行管道时,是否有任何可用的角色或标志可以将向后兼容设置为TRUE?

We recently migrated to SQL Server 2019 by provisioning a new VM. We would like to set the backward compatibility to true as our old server was in SQL Server 2012.

Is there any ansible role or flag available to set the backward compatibility to true while running the pipeline during SQL Server installation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2025-02-20 21:07:22

使用SQL SQL Server设置兼容性级别,

使用 compatible_level 设置来提供向后兼容性。设置SQL Server的兼容性级别设置通常用于单个数据库而不是整个服务器。可以使用这样的SQL命令来完成此操作:

ALTER DATABASE some_database_name SET COMPATIBILITY_LEVEL = 110;

110 是SQL Server 2012的兼容性级别。它将使数据库相当紧密地运行到SQL Server默认情况下的表现,但是会有一个或两个微妙的差异。 Alter数据库(transact-sql)兼容性级别网页。

创建数据库时,服务器主要仅复制模型数据库。因此,如果要更改该服务器上创建的所有数据库的兼容性级别,则需要在模型数据库上设置兼容性级别。

ALTER DATABASE model SET COMPATIBILITY_LEVEL = 110;

但这不是推荐的方法。

Ansible使用Ansible运行SQL

使用win_shell和Indoke-SQLCMD

没有内置的方式来管理SQL Server数据库。周围有各种各样的工作。一种是使用 win_shell:选项运行一个称为 Indoke-sqlcommand 运行SQL,类似的东西:

- name: 'Set DB compatibility level'
  win_shell: "invoke-sqlcmd -Database master -username \"{{db_user}}\" -password \"{{db_pass}}\" -Query \"ALTER DATABASE {{dbName}} SET COMPATIBILITY_LEVEL = 110;\""

这是假设您拥有 sqlps sqlserver PowerShell模块已安装。也许也可以使用命令行命令,但我将其作为练习。它可能已经安装了可能更有可能。

使用Microsoft.sql.server角色

另一种方法是使用开源 microsoft.sql .Server Ansible角色。 Microsoft发表了一篇名为部署SQL Server - Ansible方法!具有更完整的描述。但简短地可以如下完成。

该角色具有 MSSQL_INPUT_SQL_FILE:属性,该属性允许设置SQL文件。该文件将包含您要在安装时运行的任何SQL脚本。有关命令,请参见使用上面的SQL部分的设置兼容性级别。可以这样使用:

- hosts: all
  vars:
    mssql_accept_microsoft_odbc_driver_17_for_sql_server_eula: true
    mssql_accept_microsoft_cli_utilities_for_sql_server_eula: true
    mssql_accept_microsoft_sql_server_standard_eula: true
    mssql_password: "p@55w0rD"
    mssql_edition: Evaluation
    mssql_input_sql_file: my_sql_file.sql
  roles:
    - microsoft.sql.server

请注意,SQL通常不是 idempotent 。因此,通常建议在创建事物之前检查事物是否存在。但是有限制,一旦删除数据,就不会恢复。幸运的是,数据库兼容性级别是可以多次应用的几件事之一。

Setting the compatibility level using SQL

SQL Server uses a COMPATIBILITY_LEVEL setting to provide backwards compatibility. Setting the compatibility level setting of SQL Server is normally applied for an individual database rather than the whole server. This can be done using an SQL Command like this:

ALTER DATABASE some_database_name SET COMPATIBILITY_LEVEL = 110;

110 is the compatibility level for SQL Server 2012. It will make the database run fairly closely to how SQL Server 2012 behaves by default, but there will be one or two subtle differences. There is a full list of compatibility levels in the ALTER DATABASE (Transact-SQL) compatibility level web page.

When creating a database the server largely just copies the model database. So if you want to change the compatibility level for all databases created on that server going forwards you need to set the compatibility level on the model database like this.

ALTER DATABASE model SET COMPATIBILITY_LEVEL = 110;

But this is not a recommended approach.

Running SQL using ansible

using win_shell and invoke-sqlcmd

Ansible does not have a built in way to manage SQL Server databases. There are various work arounds. One is to use the win_shell: option to run a PowerShell command called invoke-sqlcommand that runs the SQL, something like this:

- name: 'Set DB compatibility level'
  win_shell: "invoke-sqlcmd -Database master -username \"{{db_user}}\" -password \"{{db_pass}}\" -Query \"ALTER DATABASE {{dbName}} SET COMPATIBILITY_LEVEL = 110;\""

This assumes you have the SQLPS or SqlServer PowerShell module installed. It is probably also possible to use the sqlcmd command line command, but I'll leave that as an exercise. It is probably slightly more likely to already be installed.

Using the microsoft.sql.server role

Another way is to use the open source microsoft.sql.server ansible role. Microsoft have published an article called Deploy SQL Server – The Ansible way! that has a fuller description. But briefly it can be done as follows.

The role has an mssql_input_sql_file: attribute that allows the setting of an SQL file. The file would contain any SQL Script you want to run on install. See the Setting the compatibility level using SQL section above for the commands. This can be used like this:

- hosts: all
  vars:
    mssql_accept_microsoft_odbc_driver_17_for_sql_server_eula: true
    mssql_accept_microsoft_cli_utilities_for_sql_server_eula: true
    mssql_accept_microsoft_sql_server_standard_eula: true
    mssql_password: "p@55w0rD"
    mssql_edition: Evaluation
    mssql_input_sql_file: my_sql_file.sql
  roles:
    - microsoft.sql.server

Note that SQL is not generally idempotent. As such it is normally advisable to check if things exist before creating them. But there are limits, once you delete data there is no reverting that. Fortunately the database compatibility level is one of the few things that can be applied multiple times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文