sql 同义词使用_使用同义词简化SQL Server代码

本文探讨了如何使用SQL Server的同义词来解决跨服务器交易时手动更改服务器名称可能导致的问题。通过创建同义词,可以使得存储过程在不同环境中的代码保持一致,简化部署过程。同时,文章提到了同义词的使用注意事项,如不能对同义词使用ALTER语句,以及在基础对象位置改变时需要重新创建等。
摘要由CSDN通过智能技术生成

sql 同义词使用

问题 (Problem)

In our environment, we use different DB servers and these servers are linked via Linked servers for cross server transactions. We use these linked servers in our stored procedures. The server names are different in different environments and we had to change the server names manually when we promote the stored procedures to other environments. There is a chance of errors while changing the server names manually. In this article let us see how can we eliminate changing the code manually.

在我们的环境中,我们使用不同的数据库服务器,并且这些服务器通过链接服务器进行链接以进行跨服务器事务。 我们在存储过程中使用这些链接的服务器。 服务器名称在不同的环境中是不同的,当我们将存储过程升级到其他环境时,我们必须手动更改服务器名称。 手动更改服务器名称时可能会出错。 在本文中,让我们看看如何消除手动更改代码的麻烦。

(Solution)

We started doing POC on making our day to day SQL Server deployments/fixes automated by using tool like Source control, Compare and few other build and deployment tools.

我们开始通过使用诸如源代码控制,比较之类的工具以及其他一些构建和部署工具来使日常SQL Server部署/修复自动化来进行POC。

But before that, we want to make our database code uniform across environments. While comparing code in different environments, we found few procedures which are different. Some of them were actual code differences and some of them were different at linked servers only.

但是在此之前,我们希望使数据库代码在不同环境之间保持统一。 在不同环境中比较代码时,我们发现很少有不同的过程。 其中一些是实际的代码差异,而某些则仅在链接服务器上不同。

Our database code is tight coupled with inked servers. Linked servers are used to query the databases on different servers. These servers can be SQL Server or any other database engine.

我们的数据库代码与墨迹服务器紧密结合。 链接服务器用于查询不同服务器上的数据库。 这些服务器可以是SQL Server或任何其他数据库引擎。

To illustrate this, I am using sample server names. All the servers are SQL Servers and a linked server is created from server 1 to server 2 on all environments.

为了说明这一点,我使用了示例服务器名称。 所有服务器都是SQL Server,并且在所有环境中都创建了从服务器1到服务器2的链接服务器。

Development Servers:

开发服务器:

  • DEV1
  • DEV1
  • DEV2
  • DEV2

QA Servers:

质量检查服务器:

  • QA1
  • QA1
  • QA2
  • QA2

Production Servers:

生产服务器:

  • PROD1
  • PROD1
  • PROD2
  • PROD2

Let us create a sample stored procedure which uses table Notes on Test database server at server DEV2.

让我们创建一个示例存储过程,该存储过程在服务器DEV2的Test数据库服务器上使用表Notes。

CREATE PROCEDURE GetNotes
AS
BEGIN
SET NOCOUNT ON
SELECT * FROM [DEV2].[Test].[dbo].Notes
END
GO

Please note this is a sample stored procedure I created to illustrate the scenario.

请注意,这是我创建的一个示例存储过程,用于说明这种情况。

Now I need to change the server’s names manually while deploying the stored procedures to QA or production servers.

现在,在将存储过程部署到QA或生产服务器时,需要手动更改服务器的名称。

Please refer to below stored procedure code in different environments.

请在不同的环境中参考以下存储过程代码。

CREATE PROCEDURE GetNotes
AS
BEGIN
SET NOCOUNT ON
SELECT * FROM [QA2].[Test].[dbo].Notes
END
GO
CREATE PROCEDURE GetNotes
AS
BEGIN
SET NOCOUNT ON
SELECT * FROM [PROD2].[Test].[dbo].Notes
END
GO

I can keep the server references in a table and use dynamic T-SQL to avoid changing the server names in stored procedures every time we deploy them. But I do not want to use dynamic t-sql code. While looking at other alternatives I found synonyms as the best alternative to this problem.

我可以将服务器引用保存在一个表中,并使用动态T-SQL避免每次部署它们时都在存储过程中更改服务器名称。 但是我不想使用动态的T-SQL代码。 在寻找其他选择时,我发现同义词是解决此问题的最佳选择。

Synonym is a database object that used as an alternative name for another database object. The referenced object can be on same server or on remote server.

同义词是一个数据库对象,用作另一个数据库对象的替代名称。 引用的对象可以在同一服务器上,也可以在远程服务器上。

Now let us create synonyms in all environments for the table Notes. In dev environment on server DEV1, I Created a synonym with name as SYN_Notes which refers to Notes table on Test database at server DEV2.

现在让我们在所有环境中为表Notes创建同义词。 在服务器DEV1上的开发环境中,我创建了一个名为SYN_Notes的同义词,它引用服务器DEV2上Test数据库上的Notes表。

Please refer to below sample script to create a synonym.

请参考下面的示例脚本来创建同义词。

CREATE SYNONYM SYN_Notes   
FOR DEV2.Test.dbo.Notes;

Similarly, In QA environment on server QA1, I created a synonym with same name as above which refers to Notes table on Test database at server QA2. Please refer to sample code to create a synonym.

同样,在服务器QA1上的QA环境中,我创建了一个同名的同义词,它引用服务器QA2上Test数据库上的Notes表。 请参考示例代码以创建同义词。

CREATE SYNONYM SYN_Notes   
FOR QA2.Test.dbo.Notes;

Similarly, In Production environment on server PROD1, I created a synonym with same name as above which refers to Notes table on Test database at server PROD2. Please refer to sample code to create synonym.

同样,在服务器PROD1上的生产环境中,我创建了一个同名的同义词,它引用服务器PROD2上Test数据库上的Notes表。 请参考示例代码以创建同义词。

CREATE SYNONYM SYN_Notes   
FOR PROD2.Test.dbo.Notes;

We can also create synonym using SQL Server management studio. To create synonym using SQL Server management studio login to the server and Navigate to databases.

我们还可以使用SQL Server Management Studio创建同义词。 要使用SQL Server Management Studio创建同义词,请登录服务器并导航到数据库。

Now Navigate to SYNONYMS folder, right click and click on create synonym

现在导航到SYNONYMS文件夹,右键单击并单击创建同义词

Input the fields and click ok to create synonym.

输入字段,然后单击确定以创建同义词。

Mandatory values are synonym name, Database name, schema and Object name.

强制值是同义词名称,数据库名称,架构和对象名称。

If you supply the Server name the synonym will refer to object in specified server else the synonym will be created with reference to the object in local server.

如果提供服务器名称,则该同义词将引用指定服务器中的对象,否则该同义词将参考本地服务器中的对象创建。

Synonyms can be created without checking the existence of referenced object. They are checked at runtime and throws error if the referenced object does not exist.

无需检查引用对象的存在就可以创建同义词。 它们在运行时进行检查,如果所引用的对象不存在,则会引发错误。

After Creating the synonym with same name to base object in different environments, I will use the synonym name instead on four-part notation in stored procedure. Please refer to below code snippet.

在不同环境中为基础对象创建具有相同名称的同义词后,我将在存储过程的四部分符号上使用同义词名称。 请参考下面的代码片段。

CREATE PROCEDURE GetNotes
AS
BEGIN
SET NOCOUNT ON
SELECT * FROM SYN_Notes
END
GO

Now the code is uniform across environments and it is easy for us to deploy the stored procedures to other environments without changing the stored procedure code every time.

现在,代码在各个环境之间是统一的,我们可以轻松地将存储过程部署到其他环境,而无需每次都更改存储过程代码。

We need to make sure the synonym is created for the referenced object before we deploy the stored procedure.

在部署存储过程之前,我们需要确保为引用的对象创建了同义词。

Not only tables synonyms can be created for other objects as well.

不仅可以为其他对象创建表同义词。

We can create synonyms for the following database objects types

我们可以为以下数据库对象类型创建同义词

  • Function

    功能
  • SQL stored procedure

    SQL存储过程
  • View

    视图
  • User defined table

    用户定义表

注意事项: (Considerations:)

Alter statement cannot be used on synonyms. We must drop and recreate them.

Alter语句不能用于同义词。 我们必须删除并重新创建它们。

/****** Object:  Synonym [dbo].[SYN_Notes]    Script Date: 1/1/2019 3:12:07 AM ******/
DROP SYNONYM [dbo].[SYN_Notes]
GO
 
/****** Object:  Synonym [dbo].[SYN_Notes]    Script Date: 1/1/2019 3:12:07 AM ******/
CREATE SYNONYM SYN_Notes   
FOR DEV2.Test.dbo.Notes;

We cannot issue DDL statement to synonym to modify base object.

我们不能发布DDL语句作为同义词来修改基础对象。

For example, like adding new column to base table. It throws an error

例如,向基础表添加新列。 引发错误

  • Msg 4909, Level 16, State 1, Line 16
    Cannot alter ‘SYN_Notes’ because it is not a table.
  • 消息4909,第16级,状态1,第16行
    无法更改'SYN_Notes',因为它不是表。

The base table name is not exposed.

基表名称未公开。

We must take care when the table location is changed like in case of server refresh or database moved to different server or any scenario that changes the location of base object. In this case we must drop the synonym and recreate them with new locations.

当更改表位置时,例如在服务器刷新或数据库移至其他服务器或任何更改基础对象位置的情况下,我们必须小心。 在这种情况下,我们必须删除同义词并使用新位置重新创建它们。

Using synonyms helps in comparing databases across environments. As this makes code uniform across servers, the stored procedures which uses synonyms does not come up in differences until unless if there is actual code difference.

使用同义词有助于在跨环境中比较数据库。 因为这使代码在服务器之间统一,所以使用同义词的存储过程不会有差异,除非存在实际的代码差异。

Synonyms can be dropped without removing its references in code. So just make sure that the synonyms are not used in the code before dropping them.

可以删除同义词而无需在代码中删除其引用。 因此,在删除同义词之前,请确保未在代码中使用同义词。

Synonym can be created once and used in multiple stored procedures.

同义词只能创建一次,并在多个存储过程中使用。

Multiple synonyms can be created with references to same object.

可以通过引用同一对象来创建多个同义词。

You can query sys.synonyms and group base_object_name to find the duplicate synonyms.

您可以查询sys.synonyms和组base_object_name来查找重复的同义词。

Truncate cannot be used on synonym like in case of table.

不能像表一样将截断用于同义词。

Below are the operations that can be used on synonyms.

以下是可用于同义词的操作。

  • SELECCT
  • 选择
  • INSERT
  • DELETE
  • 删除
  • UPDATE
  • 更新
  • EXECUTE
  • 执行

In this article I have explained below points on synonyms.

在本文中,我解释了以下有关同义词的观点。

  • How to create synonym

    如何创建同义词
  • How to drop and recreate them

    如何删除并重新创建它们
  • How to use them in procedures

    如何在程序中使用它们
  • Benefits of using synonyms and Limitations of synonyms

    使用同义词的好处和同义词的局限性

下一步 (Next Steps)

Check your database code if you are using any linked servers and try synonyms instead of four-part notation to make code uniform and simple across environments.

如果您正在使用任何链接服务器,请检查数据库代码,并尝试使用同义词而不是四部分符号来使代码在整个环境中统一且简单。

Check the execution plans with synonyms and with four-part notation and to check if there is any impact on performance when synonyms are used.

使用同义词和四部分符号检查执行计划,并检查使用同义词时是否对性能有影响。

翻译自: https://www.sqlshack.com/code-simplification-using-synonyms/

sql 同义词使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值