In this article, we will explore QUOTED_IDENTIFIER behavior, default value, and comparison with a different value. Set options at connection level control query behavior in SQL Server. The query might behave differently with different set options and their values.
在本文中,我们将探讨QUOTED_IDENTIFIER行为,默认值以及与其他值的比较。 在连接级别上控制SQL Server中的查询行为的选项。 使用不同的设置选项及其值,查询的行为可能有所不同。
Most of the developers use SET QUOTED_IDENTIFIER ON/OFF option in a stored procedure and user-defined functions. If you have not used it, SQL Server automatically adds this while scripting out the objects. This article explains the configuration and its benefits.
大多数开发人员在存储过程和用户定义的函数中使用SET QUOTED_IDENTIFIER ON / OFF选项。 如果您尚未使用它,则SQL Server在编写对象脚本时会自动添加它。 本文介绍了配置及其好处。
Let’s create a stored procedure with the following script:
让我们使用以下脚本创建一个存储过程:
CREATE PROCEDURE [dbo].[SQLTest]
AS
BEGIN
SELECT 'Rajendra';
END;
In SSMS’s Object Explorer, right-click on the stored procedure and generate the stored procedure script (Script stored procedure as -> Create To -> New Query Editor Window):
在SSMS的对象资源管理器中,右键单击存储过程并生成存储过程脚本(脚本存储过程为->创建至->新建查询编辑器窗口):
It generates the script in the new query window of SSMS. In the generated script, we can see an option SET QUOTED_IDENTIFIER ON. We have not added any such argument while creating the stored procedure. You can review the stored procedure script again for verification:
它在SSMS的新查询窗口中生成脚本。 在生成的脚本中,我们可以看到一个选项SET QUOTED_IDENTIFIER ON 。 创建存储过程时,我们没有添加任何此类参数。 您可以再次查看存储过程脚本以进行验证:
You are probably asking yourself why SSMS adds these options while generating a script for an object?
您可能会问自己,为什么SSMS在为对象生成脚本时添加这些选项?
Let’s understand the QUOTED_IDENTIFIER option in SQL Server in the next section.
在下一节中,我们将了解SQL Server中的QUOTED_IDENTIFIER选项。
We use single or double quotes in the script. Suppose we want to store employees’ name in a SQL table. The employee name might include a single quote in the name as well.
我们在脚本中使用单引号或双引号。 假设我们要在SQL表中存储员工的姓名。 员工名称中也可能包含单引号。
Let’s say customer name is O’Brien, if we try to insert this name in a table, we get the following error message:
假设客户名称为O'Brien,如果我们尝试将此名称插入表中,则会收到以下错误消息:
Insert into [Employee]
Values(
'Joe',
"O'Brien",
'UK')
Error message:
错误信息:
207, Level 16, State 1, Line 3 Invalid column name ‘O’Brien’.
207,级别16,状态1,第3行无效的列名'O'Brien'。
We can execute the query as shown below, and it inserts record successfully in the table:
我们可以执行如下所示的查询,并将查询成功插入到表中:
SET QUOTED_IDENTIFIER OFF
Insert into [Employee]
Values(
'Joe',
"O'Brien",
'UK')
We can use single quotes as well to insert the data.
我们也可以使用单引号插入数据。
QUOTED_IDENTIFIER controls the behavior of SQL Server handling double-quotes.
QUOTED_IDENTIFIER控制SQL Server处理双引号的行为。
Look at the following example with different values of QUOTED_IDENTIFIER:
请看下面的示例,使用不同的QUOTED_IDENTIFIER值:
SET QUOTED_IDENTIFIER OFF
GO
SELECT "Rajendra"
GO
It gives the output Rajendra:
它给出输出Rajendra :
Let’s turn this option ON and execute the query.
让我们将此选项打开并执行查询。
SET QUOTED_IDENTIFIER ON
GO
SELECT "Rajendra"
GO
We get an error message stating invalid column name:
我们收到一条错误消息,指出无效的列名:
We cannot use a reserved keyword for object names. For example, function is a reserved keyword, and we cannot create any existing object with this:
我们不能使用保留关键字作为对象名称。 例如, function是一个保留关键字,并且我们不能用此创建任何现有对象:
CREATE TABLE Function(id INT);
It gives the following error message:
它给出以下错误信息:
As highlighted earlier, we can use reserved keywords in the object name once we enable the QUOTED_IDENTIFIER ON and SQL Server does not check the string for any existing rules.
如前所述,一旦启用QUOTED_IDENTIFIER ON并且SQL Server不检查任何现有规则的字符串,就可以在对象名称中使用保留关键字。
In the following query, we use the reserved keyword function as an object:
在以下查询中,我们使用reserved关键字函数作为对象:
SET QUOTED_IDENTIFIER ON;
GO
CREATE TABLE dbo."Function"
(id INT,
Name VARCHAR(20)
);
GO
INSERT INTO dbo."Function"
(id,
Name
)
VALUES
(1,
'Rajendra'
);
GO
Once the object is created, we can access the object without specifying the QUOTED_IDENTIFIER option. Both queries execute successfully:
创建对象后,无需指定QUOTED_IDENTIFIER选项就可以访问该对象。 这两个查询成功执行:
SELECT id,name FROM dbo."Function"
SELECT [id],
[Name]
FROM [DemoSQL].[dbo].[Function];
Let’s view the behavior of single quotes along with QUOTED_IDENTIFIER ON and OFF:
让我们看一下单引号以及QUOTED_IDENTIFIER ON和OFF的行为:
SET QUOTED_IDENTIFIER ON;
GO
SELECT 'Rajendra';
As you can see in the following screenshot, it treats string inside single quotes as literal. In the first select statement, it prints the string inside a single quote.
如下面的屏幕快照所示,它将单引号内的字符串视为文字。 在第一个select语句中,它在单引号内打印字符串。
With the QUOTED_IDENTIFIER OFF, it gives a similar output. This setting does not impact SQL Server behavior for the single quotes:
在QUOTED_IDENTIFIER为OFF的情况下,将提供类似的输出。 此设置不影响单引号SQL Server行为:
SET QUOTED_IDENTIFIER OFF
GO
SELECT 'Rajendra'
In the following example, let’s observe a single and double quote behavior with QUOTED_IDENTIFIER:
在下面的示例中,让我们观察一下QUOTED_IDENTIFIER的单引号和双引号行为:
SET QUOTED_IDENTIFIER OFF
GO
SELECT 'Rajendra'
GO
SELECT "Rajendra"
Both Select statements return similar output once we turn off the QUOTED_IDENTIFIER option:
一旦我们关闭QUOTED_IDENTIFIER选项,两个Select语句都将返回相似的输出:
In another example, we use previous Select statements with the QUOTE_INDENTIFIER ON:
在另一个示例中,我们在QUOTE_INDENTIFIER ON上使用先前的Select语句:
SET QUOTED_IDENTIFIER ON
GO
SELECT 'Rajendra'
GO
SELECT "Rajendra"
We get an error message with the double-quotes. You can also see that SQL Server does not parse the double-quoted string. It cannot recognize the object and highlights the string with an underline:
我们收到带有双引号的错误消息。 您还可以看到SQL Server不会解析双引号字符串。 它无法识别对象,并用下划线突出显示字符串:
Let’s look at a few complex examples with a combination of single and double quotes in a string:
让我们看一些在字符串中结合使用单引号和双引号的复杂示例:
SET QUOTED_IDENTIFIER OFF;
GO
SELECT '''Rajendra''';
It contains three single quotes marks:
它包含三个单引号:
SET QUOTED_IDENTIFIER ON;
GO
SELECT '''Rajendra''';
If we use a double quote and single quote together, it treats double quote as string delimiter and prints the other single quote. We can explore a few more commands to see the behavior of QUOTED_IDENTIFIER:
如果我们同时使用双引号和单引号,它将双引号视为字符串定界符,并打印其他单引号。 我们可以探索更多命令来查看QUOTED_IDENTIFIER的行为:
SET QUOTED_IDENTIFIER OFF;
GO
SELECT "'Rajendra '''' Gupta'";
SELECT 'Rajendra '''' Gupta';
SELECT '"Rajendra Gupta"';
SELECT """Rajendra""";
SELECT "Rajendra """" Gupta";
GO
SET QUOTED_IDENTIFIER ON;
GO
SELECT "'Rajendra '''' Gupta'";
SELECT 'Rajendra '''' Gupta';
SELECT '"Rajendra Gupta"';
SELECT """Rajendra""";
SELECT "Rajendra """" Gupta";
GO
We can use sys.sql_modules to filter the results using the QUOTED_IDENTIFIER setting ON or OFF. It shows the objects in which we defined QUOTED_IDENTIFIER explicitly. It does not show the objects using the default behavior:
我们可以使用QUOTED_IDENTIFIER设置为ON或OFF来使用sys.sql_modules过滤结果。 它显示了我们在其中明确定义QUOTED_IDENTIFIER的对象。 它不使用默认行为显示对象:
Query to find objects using QUOTED_IDENTIFIER ON
使用QUOTED_IDENTIFIER ON查询查找对象
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE uses_quoted_identifier = 1
Query to find objects using QUOTED_IDENTIFIER OFF
使用QUOTED_IDENTIFIER OFF查询以查找对象
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE uses_quoted_identifier = 0
At the beginning of the article, we created a new stored procedure without specifying QUOTED_IDENTIFIER. Later, we generated the script, and it includes SET QUOTED_IDENTIFIER ON option.
在本文的开头,我们创建了一个新的存储过程,但未指定QUOTED_IDENTIFIER。 稍后,我们生成了脚本,其中包括SET QUOTED_IDENTIFIER ON选项。
We can control this behavior using SSMS settings. In SSMS, go to Tools -> Options.
我们可以使用SSMS设置来控制此行为。 在SSMS中,转到工具->选项。
It gives various options to control SSMS behavior such as environment details, query execution, query results behavior:
它提供了各种选项来控制SSMS行为,例如环境详细信息,查询执行,查询结果行为:
It also has a search option in that we can filter out the required configuration. Search for keyword ANSI, and it gives you settings to control query execution behavior.
它还有一个搜索选项,我们可以过滤出所需的配置。 搜索关键字ANSI,它为您提供了控制查询执行行为的设置。
In this screenshot, we can see it has an option SET QUOTED_IDENTIFIER, and it is enabled. It is the default behavior of SQL Server:
在此屏幕截图中,我们可以看到它具有选项SET QUOTED_IDENTIFIER,并且已启用。 这是SQL Server的默认行为:
If we want to turn off QUOTE_IDENIFIER, we can remove the tick from the checkbox:
如果要关闭QUOTE_IDENIFIER,可以从复选框中删除勾号:
Click OK and restart the SSMS so that this setting can take effect. Let’s create the procedure [dbo].[SQLTest] again and generate the script for it. In the below screenshot, we can see that it has QUOTED_IDENTIFIER OFF now:
单击“ 确定”,然后重新启动SSMS,以使此设置生效。 让我们再次创建过程[dbo]。[SQLTest]并为其生成脚本。 在下面的屏幕截图中,我们可以看到它现在已关闭QUOTED_IDENTIFIER:
Note: If we change the QUOTED_IDENTIFIER option SSMS tools, it turns off the QUOTED_IDENTIFIER for all client session. You should be careful about modifying this option in the production environment.
注意:如果我们更改QUOTED_IDENTIFIER选项SSMS工具,它将关闭所有客户端会话的QUOTED_IDENTIFIER。 您应该在生产环境中修改此选项时要小心。
We can use the SQL execution plan to view the current value of QUOTED_IDENTIFIER. Enable Actual execution plan before executing the query (shortcut key – CTRL+M):
我们可以使用SQL执行计划来查看QUOTED_IDENTIFIER的当前值。 在执行查询之前启用实际执行计划(快捷键– CTRL + M):
SET QUOTED_IDENTIFIER OFF;
GO
SELECT TOP (1000) [Firstname]
,[Lastname]
,[Country]
FROM [DemoSQL].[dbo].[Employee]
We executed this query by specifying QUOTE_INDENTIFIER value OFF, let’s view the execution plan. Under the execution plan properties, we can see the Set option and its current value in the query:
我们通过将QUOTE_INDENTIFIER值指定为OFF来执行此查询,让我们查看执行计划。 在执行计划属性下,我们可以在查询中看到Set选项及其当前值:
Parameter |
QUOTED_IDENTIFIER ON |
QUOTED_IDENTIFIER OFF |
Behaviour |
Default |
Not default |
The string within double quotes |
It treats a string as an Identifier:
It gives the following error message: Msg 207, Level 16, State 1, Line 4 Invalid column name ‘Rajendra’. |
It treats the string as literal. This statement works fine: |
Use reserved keyword |
We can use a reserved keyword in the object name within double-quotes. |
We cannot use the reserved keyword. We get an error message – Incorrect syntax error (102, Level 15, State 1) |
Identify objects |
If we define explicitly QUOTED_IDENTIFIER value, we can find it using the following query:
|
We can use the following query for finding objects with QUOTED_IDENTIFIER OFF: |
参数 |
QUOTED_IDENTIFIER ON |
QUOTED_IDENTIFIER已关闭 |
行为 |
默认 |
不默认 |
双引号内的字符串 |
它将字符串视为标识符: 它给出以下错误信息: 消息207,级别16,状态1,第4行无效的列名称“ Rajendra”。 |
它将字符串视为文字。 该语句可以正常工作: |
使用保留关键字 |
我们可以在双引号中的对象名称中使用保留关键字。 |
我们不能使用reserved关键字。 我们收到一条错误消息–不正确的语法错误(102,级别15,状态1) |
识别物体 |
如果我们显式定义QUOTED_IDENTIFIER值,则可以使用以下查询找到它: |
我们可以使用以下查询查找具有QUOTED_IDENTIFIER OFF的对象: |
This article gives an overview of the QUOTED_IDENTIFIER set option and its behavior in SQL Server queries with single and double-quotes. We should be careful while changing the default value. You should change as per requirement only.
本文概述了QUOTED_IDENTIFIER设置选项及其在带单引号和双引号SQL Server查询中的行为。 更改默认值时应小心。 您仅应按要求进行更改。
翻译自: https://www.sqlshack.com/set-quoted_identifier-settings-in-sql-server/