SET QUOTED IDENTIFIER should be ON when inserting a record

前端 未结 4 1596
一整个雨季
一整个雨季 2020-12-06 12:00

I am stuck in a rather strange problem with SQL Server 2005, which throws

\"SET QUOTED IDENTIFIER should be on when inserting record\"

相关标签:
4条回答
  • 2020-12-06 12:05

    After a long struggle we were able to fix this problem. I just wanted to share the reason.

    Our build team maintains a separate in-house tool to deploy scripts, which internally triggers the SQLCMD (shell) utility to execute T-SQL scripts in a db.

    Here is the culprit: by default, QUOTED_IDENTIFIER is OFF when running in SQLCMD mode!

    Every script run through this tool is created with QUOTED IDENTIFIER OFF. We are the only module which uses indexed views. All the remaining stories you know well in my previous posts :(

    NOTE: I am going to vote everyone's post as useful.

    0 讨论(0)
  • 2020-12-06 12:20

    Script the stored proc, ensure/change SET options, run the ALTER PROC to ensure SET QUOTED IDENTIFIER ON is set.

    Why?

    The setting of "SET QUOTED IDENTIFIER" is defined at creation time for stored procs and is always "ON" for tables. Source, BOL.

    When a table is created, the QUOTED IDENTIFIER option is always stored as ON in the table's metadata even if the option is set to OFF when the table is created.

    When a stored procedure is created, the SET QUOTED_IDENTIFIER and SET ANSI_NULLS settings are captured and used for subsequent invocations of that stored procedure.

    The default for connections can be defined at the server level (sp_configure 'user options') or database level (ALTER DATABASE). For SSMS, it's under "Tools..Options.. Query Execution..SQL Server..ANSI". It's also the default for client libraries too (except DB-LIb).

    Now, it you open an SSMS Query Window and start typing "CREATE PROC.." then it uses SSMS settings when you run the code.

    And SET QUOTED IDENTIFIER can not be set at run time inside the stored proc. Show me the a reference before you disagree... From the MS BOL link above:

    When executed inside a stored procedure, the setting of SET QUOTED_IDENTIFIER is not changed.

    You have to work hard to run any code with this OFF... so the most likely fix is to ALTER or re-create the stored proc.

    0 讨论(0)
  • 2020-12-06 12:22

    In SQL Server 2005, SET QUOTED IDENTIFIER is OFF by default, not ON (unless using an ODBC or OLE connection...see this for more information).

    You do not need to create the table with SET QUOTED IDENTIFIER ON to use it.

    All you need to do is add SET QUOTED IDENTIFIER ON to the beginning of your SP to enable it for the run of the procedure (and make sure that if you don't wish to leave it on, you have SET QUOTED IDENTIFIER OFF to switch it back).

    EDIT

    I stand corrected. According to this MSDN Page, SET QUOTED IDENTIFIER is ON by default (unless connection with a DB-Library application.

    0 讨论(0)
  • 2020-12-06 12:29

    I was just reading this article by Erland Sommarskog, The Curse and Blessings of Dynamic SQL, and it includes the following paragraph in regards to the SET QUOTED IDENTIFIER setting:

    The default for this setting depends on context, but the preferred setting is ON, and it must be ON in order to use XQuery, indexed views and indexes on computed columns.

    Does your stored procedure make use of XQuery, indexed views or indexes on computed columns at all?

    0 讨论(0)
提交回复
热议问题