QUOTED_IDENTIFIER 选项对 index的影响

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QUOTED_IDENTIFIER 选项对 index的影响相关的知识,希望对你有一定的参考价值。

SET QUOTED_IDENTIFIER 选项对Index的影响

  1. 当创建或修改的index包含computed columns ,必须 SET QUOTED_IDENTIFIER=ON;
  2. 当创建或修改Indexed View上的Index时,必须 SET QUOTED_IDENTIFIER=ON;
  3. 当创建或修改filtered index时,必须 SET QUOTED_IDENTIFIER=ON;

一,Case

早上,发现用于处理Index fragmentation的Job跑失败了,查看job history,发现以下Error:

ALTER INDEX failed because the following SET options have incorrect settings: ‘QUOTED_IDENTIFIER‘. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations. [SQLSTATE 42000] (Error 1934).  The step failed.

错误原因是:没有 SET QUOTED_IDENTIFIER =ON。

MSDN的解释是:

SET QUOTED_IDENTIFIER must be ON when you are creating or changing indexes on computed columns or indexed views. If SET QUOTED_IDENTIFIER is OFF, CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail.

SET QUOTED_IDENTIFIER must be ON when you are creating a filtered index.

SET QUOTED_IDENTIFIER must be ON when you invoke XML data type methods.

二,SET QUOTED_IDENTIFIER 选项的解释

When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks.

When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. Literals can be delimited by either single or double quotation marks.

 

当设置QUOTED_IDENTIFIER时,使用双引号的字符串都会被解释为一个 Object 的标识,因此,如果要标识一个字符串,必须使用单引号。

When SET QUOTED_IDENTIFIER is ON (default), all strings delimited by double quotation marks are interpreted as object identifiers. Therefore, quoted identifiers do not have to follow the Transact-SQL rules for identifiers. They can be reserved keywords and can include characters not generally allowed in Transact-SQL identifiers. Double quotation marks cannot be used to delimit literal string expressions; single quotation marks must be used to enclose literal strings. If a single quotation mark () is part of the literal string, it can be represented by two single quotation marks ("). SET QUOTED_IDENTIFIER must be ON when reserved keywords are used for object names in the database.

When SET QUOTED_IDENTIFIER is OFF, literal strings in expressions can be delimited by single or double quotation marks. If a literal string is delimited by double quotation marks, the string can contain embedded single quotation marks, such as apostrophes.

 

1,delimiting identifiers

当 SET QUOTED_IDENTIFIER=ON时,SQL Server将双引号作为界定符,功能和默认定界符 中括号 [] 相同。

而中括号作为定界符是不受Quoted_Identifier 选项设置的影响的,始终可以作为定界符使用。

Using brackets, [ and ], to delimit identifiers is not affected by the QUOTED_IDENTIFIER setting.

 

select是关键字,不能用于用户定义的object,除非使用定界符。当SET QUOTED_IDENTIFIER ON 时,可以使用双引号“select”,这样select关键字就能作为Table Name,作用和[select]相同。

SET QUOTED_IDENTIFIER OFF
GO
-- An attempt to create a table with a reserved keyword as a name
-- should fail.
CREATE TABLE "select" ("identity" INT IDENTITY NOT NULL, "order" INT NOT NULL);
GO

SET QUOTED_IDENTIFIER ON;
GO

-- Will succeed.
CREATE TABLE "select" ("identity" INT IDENTITY NOT NULL, "order" INT NOT NULL);
GO

SELECT "identity","order" 
FROM "select"
ORDER BY "order";
GO

DROP TABLE "SELECT";
GO

SET QUOTED_IDENTIFIER OFF;
GO

2,当 SET QUOTED_IDENTIFIER=ON时,字符串必须使用单引号

当SET QUOTED_IDENTIFIER=OFF时,字符串可以使用使用单引号,也可以使用双引号

SET QUOTED_IDENTIFIER ON;
GO
--success
DECLARE @var varchar(10)
set @var=abc
select @var
GO

--failure
DECLARE @var varchar(10)
set @var="abc"
select @var
GO


SET QUOTED_IDENTIFIER OFF;
GO
--success
DECLARE @var varchar(10)
set @var=abc
select @var
GO

--success
DECLARE @var varchar(10)
set @var="abc"
select @var
GO

报错信息:非法的Column Name,很奇怪的错误信息。当设置QUOTED_IDENTIFIER时,双引号标识的字符串会被解释为一个 Object 的标识。
Msg 207, Level 16, State 1, Line 4
Invalid column name ‘abc‘.

 

二, 从 sys.databases 中查看到Quoted_Identifier 选项的设置

select db.name,db.database_id,  db.is_quoted_identifier_on
from sys.databases db

三, SET QUOTED_IDENTIFIER 选项对Index的影响

  1. 当创建或修改的index包含computed columns ,必须 SET QUOTED_IDENTIFIER=ON;
  2. 当创建或修改Indexed View上的Index时,必须 SET QUOTED_IDENTIFIER=ON
  3. 当创建或修改filtered index时,必须 SET QUOTED_IDENTIFIER=ON;

SET QUOTED_IDENTIFIER must be ON when you are creating or changing indexes on computed columns or indexed views. If SET QUOTED_IDENTIFIER is OFF, CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail. For more information about required SET option settings with indexed views and indexes on computed columns, see "Considerations When You Use the SET Statements" in SET Statements (Transact-SQL).

SET QUOTED_IDENTIFIER must be ON when you are creating a filtered index.

SET QUOTED_IDENTIFIER must be ON when you invoke XML data type methods.

The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server automatically set QUOTED_IDENTIFIER to ON when connecting. This can be configured in ODBC data sources, in ODBC connection attributes, or OLE DB connection properties. The default for SET QUOTED_IDENTIFIER is OFF for connections from DB-Library applications.

When a table is created, the QUOTED IDENTIFIER option is always stored as ON in the table‘s metadata even if the option is set to OFF when the table is created.

When a stored procedure is created, the SET QUOTED_IDENTIFIER and SET ANSI_NULLS settings are captured and used for subsequent invocations of that stored procedure.

When executed inside a stored procedure, the setting of SET QUOTED_IDENTIFIER is not changed.

When SET ANSI_DEFAULTS is ON, SET QUOTED_IDENTIFIER is enabled.

SET QUOTED_IDENTIFIER also corresponds to the QUOTED_IDENTIFIER setting of ALTER DATABASE. For more information about database settings, see ALTER DATABASE (Transact-SQL).

四,Scope

Set 语句只会影响当前的Session,并且是在Parse 时设置的。

The Transact-SQL programming language provides several SET statements that change the current session handling of specific information.

SET QUOTED_IDENTIFIER is set at parse time. Setting at parse time means that if the SET statement is present in the batch or stored procedure, it takes effect, regardless of whether code execution actually reaches that point; and the SET statement takes effect before any statements are executed. When multiple conflicting SET statements are present in the batch, the last setting parsed is used.

五,Summary

在创建或修改Index时,记得在语句前面加上:SET QUOTED_IDENTIFIER ON,有利无害。

 

参考文档:

SET QUOTED_IDENTIFIER (Transact-SQL)

以上是关于QUOTED_IDENTIFIER 选项对 index的影响的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server:INSERT/UPDATE/DELETE 失败,因为以下 SET 选项的设置不正确:'QUOTED_IDENTIFIER'

全局实施 QUOTED_IDENTIFIER

SQL Server 存储过程因使用 XML/ANSI_NULLS、QUOTED_IDENTIFIER 选项而失败

在存储过程中设置 QUOTED_IDENTIFIER

架构比较 SET QUOTED_IDENTIFIER for View 与索引

过滤的唯一索引导致更新失败,因为不正确的 'QUOTED_IDENTIFIER' 设置