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)

时间: 2024-09-28 18:33:07

QUOTED_IDENTIFIER 选项对 index的影响的相关文章

oracle中add&split partition对global&local index的影响

生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测试版本为oracle11.2.0.4,过程如下: 首先,创建分区表: CREATE TABLE TP1 ( C1 INT PRIMARY KEY, C2 VARCHAR2(10), C3 CHAR(10) ) partition by range (c1) ( partition p1 values less

Lucene——Field.Store(存储域选项)及Field.Index(索引选项)

Field.Store.YES或者NO(存储域选项) 设置为YES表示或把这个域中的内容完全存储到文件中,方便进行文本的还原 设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完全还原(doc.get) Field.Index(索引选项) Index.ANALYZED:进行分词和索引,适用于标题.内容等 Index.NOT_ANALYZED:进行索引,但是不进行分词,如果身份证号.姓名.ID等,适用于精确搜索 Index.ANALYZED_NOT_NORMS:进行分词但是不

git commit的--amend选项

git commit --amend常常用来修改某个branch上最顶端的commit,大多数情况下,这个命令给人的感觉是用新的commit替换了原来的commit.git commit --amend与下面的语句等价: git reset --soft HEAD^ //将branch的头指针向前移动一个commit,--soft选项使得index和workspace tree的内容保持移动之前不变 ...do something... git commit -c ORIG_HEAD //-c选

执行计划中常见index访问方式(转)

近期有朋友对于单个表上的index各种情况比较模糊,这里对于单个表上,单个index出现的大多数情况进行了总结性测试,给出了测试结果,至于为什么出现这样的试验结果未做过多解释,给读者留下思考的空间.本篇文章仅仅是为了测试hint对index的影响,而不是说明走各种index方式的好坏.参考: INDEX FULL SCAN vs INDEX FAST FULL SCAN创建表模拟测试 SQL> create table t_xifenfei as select object_id,object_

套接口选项

设置和影响套接口选项的方法有 1.getsockopt和setsockopt 2.fcntl 3.ioctl getsockopt和setsockopt 这两个方法仅适用于套接字 有两种基本类型的套接口选项:打开或关闭某个特性的二进制标志,取得并返回我们可以设置或检验的特定值的选项,标有标志的列指明是否为标志选项,对于这些项,0表示关闭标志,非0表示打开标志. 并不是所有的系统的套接字都支持所有的选项,必要时候自行验证一番. 套接字的不支持分为两种 1.未实现相关的定义,比如SO_REUSEPO

vsftp的设置选项

设置匿名用户上传的文件的权限: anon_umask= 匿名用户新增文件的umask 数值.默认值为077. VSFTPD的设置选项 VSFTPD的配置文件/etc/vsftpd/vsftpd.conf是个文本文件.以“#”字符开始的行是注释行.每个选项设置为一行,格式为“option=value”,注意“=”号两边不能留空白符.除了这个主配置文件外,还可以给特定用户设定个人配置文件,具体介绍见后.  VSFTPD包中所带的vsftpd.conf文件配置比较简单,而且非常偏执狂的(文档自称).我

获取列表菜单的选项值与选项以后的VALUE

<html> <body> <select id="izan" name="" onchange='izzzz()'> <option value="I love you!">我爱你</option> <option value="I like you!">我喜欢你</option> </select> <script typ

SQL Server 索引知识-应用,维护

创建聚集索引 a索引键最好唯一(如果不唯一会隐形建立uniquier列(4字节)确保唯一,也就是这列都会复制到所有非聚集索引中) b聚集索引列所占空间应尽量小(否则也会使非聚集索引的空间变大) c聚集索引应固定,不能随便改动(否则会引起分页,碎片,非聚集索引被迫修改等一些列问题) d聚集索引键一般与主键(primary key)分开(基于方便业务调整,如业务逻辑存储数据与clustered index key一致且不会改变,则主键可为聚集索引) 应用实例: 选择聚集键时,尽量避免因插入引起的分页

SQL语句-创建索引

  语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO USE 库名GOIF EXISTS (SELECT * FROM SYSINDEXES WHERE NAME='IX_TEST_TNAME')--检测是否已经存在IX_TEST_TNAME索引DROP INDEX TEST.IX_TEST_TNAME--如果存在则删除 --创建索引CREATE NONCLUSTERED INDEX IX_TEST_TNAME