Published on

October 9, 2019

Understanding QUOTED_IDENTIFIER in SQL Server

In SQL Server, the QUOTED_IDENTIFIER set option controls the behavior of how SQL Server handles single and double quotes in queries. This option can have a significant impact on the execution and results of your queries. In this article, we will explore the behavior of QUOTED_IDENTIFIER, its default value, and how it affects the comparison with different values.

When scripting out objects in SQL Server, such as stored procedures or user-defined functions, you may have noticed that the generated script includes the SET QUOTED_IDENTIFIER ON option. This option is automatically added by SQL Server and is commonly used by developers to ensure proper handling of quotes in their scripts.

Let’s take a look at an example to understand the importance of QUOTED_IDENTIFIER. Suppose we have a table called “Employee” and we want to insert a record with the name “O’Brien”. If we try to insert this value without setting QUOTED_IDENTIFIER OFF, we will encounter an error due to the single quote in the name. However, if we set QUOTED_IDENTIFIER OFF before the insert statement, the record will be inserted successfully.

The behavior of QUOTED_IDENTIFIER can be controlled by explicitly setting it ON or OFF. When QUOTED_IDENTIFIER is OFF, SQL Server treats the value inside double quotes as a string and does not check for any rules or reserved keywords. On the other hand, when QUOTED_IDENTIFIER is ON (which is the default setting), SQL Server treats values inside double quotes as an identifier and checks for any existing rules or reserved keywords.

It’s important to note that QUOTED_IDENTIFIER only affects the behavior of double quotes and has no impact on single quotes. Single quotes are always treated as literal strings regardless of the QUOTED_IDENTIFIER setting.

Here are a few key points to remember about QUOTED_IDENTIFIER:

  • QUOTED_IDENTIFIER ON is the default setting in SQL Server.
  • QUOTED_IDENTIFIER OFF treats values inside double quotes as strings.
  • QUOTED_IDENTIFIER ON treats values inside double quotes as identifiers and checks for rules and reserved keywords.
  • QUOTED_IDENTIFIER can be explicitly set ON or OFF in the script or at the connection level.
  • Changing the QUOTED_IDENTIFIER setting in SSMS tools affects all client sessions, so it should be used with caution in a production environment.

It’s important to understand the behavior of QUOTED_IDENTIFIER in SQL Server to ensure the correct execution and results of your queries. By properly setting this option, you can avoid errors and ensure consistent behavior in your scripts.

Thank you for reading this article on understanding QUOTED_IDENTIFIER in SQL Server. We hope you found it informative and helpful in your SQL Server development.

Click to rate this post!
[Total: 0 Average: 0]
, , , , , , , , ,

Let's work together

Send us a message or book free introductory meeting with us using button below.