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 同义词使用

sql 同义词使用_使用同义词简化SQL Server代码相关推荐

  1. mysql+sql语句赋值语句_超详细的SQL语句语法汇总

    一.数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句)INSERT INTO 表名(字段名1, 字段名2, --) VALUES ( 值1, 值2, --); INSER ...

  2. sql azure 语法_什么是Azure SQL Cosmos DB?

    sql azure 语法 介绍 (Introduction) In the Azure Portal, you will find the option to install Azure SQL Co ...

  3. python中引入sql的优点_引用sql-和引用sql相关的内容-阿里云开发者社区

    bboss持久层改进支持模块sql配置文件引用其它模块sql配置文件中sql语句 bboss持久层改进支持模块sql配置文件引用其它模块sql配置文件中sql语句. 具体使用方法如下: <pro ...

  4. java中sql模糊查询_模糊查询的sql语句(java模糊查询sql语句)

    模糊查询的sql语句(java模糊查询sql语句) 2020-07-24 11:06:02 共10个回答 假设表名为product,商品名为name,简界为remark.则可如下写:select[na ...

  5. sql delete删除列_现有表操作中SQL DELETE列概述

    sql delete删除列 In this article, we will explore the process of SQL Delete column from an existing tab ...

  6. java.sql.date格式化_如何将java.sql.date格式化为这种格式:“MM-dd-yyyy”?

    我需要以下面的格式"MM-dd-yyyy"获取 java.sql.date,但是我需要它来保留java.sql.date所以我可以将它作为日期字段放入表中.因此,格式化后它不能是S ...

  7. 修改mysql导入sql大小限制_修改phpMyAdmin导入SQL文件的大小限制

    用phpMyAdmin导入mysql数据库时,我的19M的数据库不能导入,提示mysql数据库最大只能导入2M. phpMyAdmin数据库导入出错: You probably tried to up ...

  8. sql 分组求和_从零学会SQL:汇总分析 D3

    2020年3月24日 星期二 大纲: 汇总分析: 分组: 对分组结果指定条件: 用sql解决业务问题: 对查询结果排序: 如何看懂报错信息. 1 汇总分析 汇总函数 count:求某列的行数: 计算教 ...

  9. mysql的sql执行原理图_性能测试MySQL之SQL运行原理

    一,MySQL运行原理 两个一样的图 1,SQL语句执行的过程详细说明 如上图所示,当向MySQL发送一个请求的时候,MySQL到底做了什么: a, 客户端发送一条查询给服务器. b, 服务器先检查查 ...

  10. mysql超大sql怎么还原_关于Mysql 大型SQL文件快速恢复方案

    在使用Mysql数据库的过程中,经常需要使用到备份和恢复数据库,最简单便捷的方法便是通过导出SQL数据文件和导入SQL数据文件来完成备份和恢复,但是随着项目的增长,数据量越来越大,每次恢复就成了一件很 ...

最新文章

  1. Tensorflow 源码安装成功,导入报错 ImportError: cannot import name 'build_info'
  2. GBDT与XGBoost区别与联系 GBDT近些年来更因为被用于构建搜索排序的机器学习模型而引起广泛的关注
  3. 如何快速学习freemarker以及使用经验
  4. 优秀!5部顶级数学纪录片,假期看剧涨知识必备!
  5. 利用java反射原理写了一个简单赋值和取值通用类【改】
  6. 个性互踩留言代码: 『寶貝』駕到,{豬頭}.接駕
  7. ftp服务器支持ipv6,IPV6下搭建FTP服务器的步骤
  8. PCI驱动框架简单分析
  9. JavaSE基础阶段学习笔记
  10. 【算法】分治策略:芯片测试
  11. 在Maven中出现Could not find artifact ...:pom:0.0.1-SNAPSHOT and ‘parent.relativePath‘的错误怎么解决?
  12. JavaWeb新闻项目(查询阅读主题维护)
  13. mac版idea下载(亲测有效)
  14. python的scripts里没有pip_python的scripts文件夹无pip等文件解决方法
  15. 算法 · 深入理解 Fibonacci 数列计算及黄金分割
  16. python thread daemon
  17. 如何成功抵御DOS攻击?给你介绍四种方法
  18. 百度携手山西阳泉,打造全国首批中小型智能城市示范样本
  19. RCode对比RStudio使用体验
  20. Riak - 背景篇(1)

热门文章

  1. 前后端交互过程、常见软件架构、服务器分类
  2. 哈希值定义,哈希值是怎么生成的
  3. 怎么修改电脑的ip地址
  4. Linux内核网络UDP数据包发送(四)——Linux netdevice 子系统
  5. 本地代码上传github
  6. 利用python通过两点构成的空间直线和平面计算交点
  7. c语言case后语句,switch语句中case后的标号是什么
  8. Hive 安全模式下 Prepare Plan 的时间分析
  9. mysql事务prepare_mysql之 事务prepare 与 commit 阶段分析
  10. oracle序时账是什么,​序时账和明细账区别是什么