原文:
SQL点滴1—SET QUOTED_IDENTIFIER OFF语句的作用
先看下面几个sql语句
代码
1
SET
QUOTED_IDENTIFIER
ON
2 SELECT * FROM " USER " WHERE a = ' netasp '
3
4 SET QUOTED_IDENTIFIER ON
5 SELECT * FROM [ USER ] WHERE a = ' netasp '
6
7 SET QUOTED_IDENTIFIER OFF
8 SELECT * FROM [ USER ] WHERE a = "netasp"
9
10 SET QUOTED_IDENTIFIER OFF
11 SELECT * FROM [ USER ] WHERE a = ' netasp '
2 SELECT * FROM " USER " WHERE a = ' netasp '
3
4 SET QUOTED_IDENTIFIER ON
5 SELECT * FROM [ USER ] WHERE a = ' netasp '
6
7 SET QUOTED_IDENTIFIER OFF
8 SELECT * FROM [ USER ] WHERE a = "netasp"
9
10 SET QUOTED_IDENTIFIER OFF
11 SELECT * FROM [ USER ] WHERE a = ' netasp '
当在数据库中新建一个名字是USER的表的时候,常常会带来一些麻烦,因为USER是SQL中的关键字,但是上面的几个语句不会报错。再说一个概念:标示符是SQL中的中括号[]。
当SET QUOTED_IDENTIFIER值为ON时,双引号内的字符被当作是数据库对象。就是说双引号" "和标识符[]效果是一样样的,他们都表示引用的字符是数据库对象。单引号'表示字符串的边界。
当SET QUOTDE_IDENTIFIER OFF时,双引号被解释为字符串的边界,和单引号的作用是类似的。就是说双引号"不能当做标识符使用,但是可以当做字符边界,和单引号'的效果是一样样的。
可以做一个总结:当SET QUOTED_IDENTIFIER ON " "等同于[ ] 表示数据库对象;当SET QUOTED_IDENTIFIER OFF " "等同于' '表示字符串边界;还有这里的双引号" 并不是两个单引号'合起来的,是shift+”打出来的,初学者可能会犯这样的错误。