Wednesday, 19 July 2017

SET QUOTED_IDENTIFIER ON vs SET QUOTED_IDENTIFIER OFF

What are the differences between SET QUOTED_IDENTIFIER ON and SET QUOTED_IDENTIFIER OFF [SET QUOTED_IDENTIFIER ON vs SET QUOTED_IDENTIFIER OFF]?

SET QUOTED_IDENTIFIER ON
SET QUOTED_IDENTIFIER OFF
Use
It is treated as IDENTIFIER
Allows characters enclosed with double quotes[“”] for Object names
It is treated as Literal OR Value 
Returns the string value enclosed with double quotes[“”] as value
SET QUOTED_IDENTIFIER ON
CREATE TABLE "Child"(RecordId int IDENTITY(100,1))
GO

RESULT:
Command(s) completed successfully.

SET QUOTED_IDENTIFIER OFF
CREATE TABLE "Child"(RecordId int IDENTITY(100,1))
GO

RESULT:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'Child'.


CREATE TABLE Child(RecordId int IDENTITY(100,1))
GO

INSERT Child DEFAULT VALUES
GO

SET QUOTED_IDENTIFIER ON
SELECT "RecordId" FROM Child
RESULT:
RecordId
100


CREATE TABLE Child(RecordId int IDENTITY(100,1))
GO

INSERT Child DEFAULT VALUES
GO

SET QUOTED_IDENTIFIER OFF
SELECT "RecordId" FROM Child
RESULT:
RecordId
SET QUOTED_IDENTIFIER ON
SELECT "RecordId"
RESULT:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'RecordId'.
SET QUOTED_IDENTIFIER OFF
SELECT "RecordId"
RESULT:
RecordId
Best practice is to use the SET QUOTED_IDENTIFIER ON setting.
Avoid using SET QUOTED_IDENTIFIER OFF setting. As more and more new features require this setting value to be ON. For example while working with Filtered Indexes SET QUOTED_IDENTIFIER setting should be ON otherwise we will get into an exception.
Objects which are created with SET QUTOED IDENTIFIER ON/OFF
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE uses_quoted_identifier = 1
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE uses_quoted_identifier = 0

No comments:

Post a Comment