Groups | Blog | Home
all groups > sql server (alternate) > january 2004 >

sql server (alternate) : SET QUOTED_IDENTIFIER OFF


theintrepidfox NO[at]SPAM hotmail.com
1/14/2004 8:56:32 PM
Hello!
How bad is it to use SET QUOTED_IDENTIFIER OFF before an INSERT and
then switch it ON straight afterwards. So I can insert strings like
O'Neil etc.
It's my only design option at the moment. Everything works fine and as
far as I can see, it's only set to OFF for the current
connection/session so no other users will run into problems at the
same time if they need it ON, right?

Steve Jorgensen
1/15/2004 5:24:37 AM
[quoted text, click to view]

Why is this an issue?

If you're dealing with a string literal in a stored procedure or query
analyzer, you can just type a double-apostrophe to get a single apostrophe in
the string. If you're passing data from the client, you should use
parameters, and let ADO or ODBC handle passing the data properly to SQL
Server. The only other case I can think of is when you're building dynamic
SQL, and then you can use the replace function to convert ' into '' so it will
Dan Guzman
1/15/2004 2:29:52 PM
Regardless of the literal character enclosure you use, you'll have problems
when you build SQL statements with character literals that include the
enclosure character. These need to be escaped with 2 consecutive enclosure
characters. For example:

SET QUOTED_IDENTIFIER OFF
SELECT "This is a "double-quoted" string"
--Incorrect syntax near the keyword 'double'.
GO
SELECT "This is a ""double-quoted"" string"
--works
GO

SET QUOTED_IDENTIFIER ON
SELECT 'This is a 'single-quoted' string'
--Line 2: Incorrect syntax near '-'.
GO
SELECT 'This is a ''single-quoted'' string'
--works
GO

QUOTED_IDENTIFIER ON is preferred because this is the ANSI standard. In the
case of SQL Server, this is one of the SET options that must be on in order
to use indexes on computed columns and views effectively so I suggest you
standardize on QUOTED_IDENTIFIER ON. You can make your application code
cleaner if you use parameters so that the API to takes care of this for you.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Erland Sommarskog
1/15/2004 11:27:30 PM
Martin (theintrepidfox@hotmail.com) writes:
[quoted text, click to view]

In addition to the comments from Steve and Dan, note that this will not
work in a stored procedure, trigger or user-defined function because in
this case the setting that was in force when the procedure created will
apply, and the statement in the procedure will have no effect.

I have however, occasionally written dynamic SQL which starts with
SET QUOTED_IDENTIFIER, which saves me from the hassle of nested quotes.
There are however other alternatives for this situation, like using
the built-in funcion quotename().


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
theintrepidfox NO[at]SPAM hotmail.com
1/19/2004 5:15:42 AM
Thanks guys for answering my question.
I've spent some time on Erland's site and came to the conclusion that
I better follow a wise man's advice!

Have a nice day!

[quoted text, click to view]
AddThis Social Bookmark Button