We just ran into a problem with one of our stored procs throwing an error;
SELECT failed because the following SET options have incorrect settings: \'QUOTED_
Looking for an understanding of QUOTED_IDENTIFIER
i will post some understanding here.
ANSI demanded that quotation marks be used around identifiers (not around strings). SQL Server supported both:
SQL Server originally:
SELECT "Hello, world!"
--quotation markSELECT 'Hello, world!'
--apostropheCREATE TABLE [The world's most awful table name] ([Hello, world!] int)
SELECT [Hello, world!] FROM [The world's most awful table name]
ANSI (i.e. SET QUOTED_IDENTIFIER ON
):
SELECT "Hello, world!"
--quotation mark no longer valid in ANSI around stringsSELECT 'Hello, world!'
--apostropheCREATE TABLE "The world's most awful table name" ("Hello, world!" int)
SELECT "Hello, world!" FROM "The world's most awful table name"
Originally, SQL Server allowed you to use quotation marks ("..."
) and apostrophes ('...'
) around strings interchangeably (like Javascript does):
SELECT "Hello, world!"
--quotation markSELECT 'Hello, world!'
--apostropheAnd if you wanted a name table, view, procedure, column etc with something that would otherwise violate all the rules of naming objects, you could wrap it in square brackets ([
, ]
):
CREATE TABLE [The world's most awful table name] ([Hello, world!] int)
SELECT [Hello, world!] FROM [The world's most awful table name]
And that all worked, and made sense.
Then ANSI came along and had other ideas:
"..."
)'...'
) for stringsWhich means that if you wanted to "quote" a funky column or table name you must use quotation marks:
SELECT "Hello, world!" FROM "The world's most awful table name"
If you knew SQL Server, you knew that quotation marks were already being used to represent strings. If you blindly tried to execute that ANSI-SQL as though it were T-SQL: it's nonsense, and SQL Server told you so:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'The world's most awful table name'.
So Microsoft added a feature to let you opt-in to the ANSI flavor of SQL.
Original
SELECT "Hello, world!" --valid
SELECT 'Hello, world!' --valid
SET QUOTED_IDENTIFIER ON
SELECT "Hello, world!" --INVALID
SELECT 'Hello, world!' --valid
SQL Server still lets you use [square brackets]
, rather than forcing you to use "quotatio marks"
. But with QUOTED_IDENTIFIER ON, you cannot use "double quote quotation mark around strings"
, you must only use 'the single quote apostrophe'
.