Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Stored Procedure Fails due to use of XML/ ANSI_NULLS, QUOTED_IDENTIFIER options

I have a stored procedure which takes an XML parameter and inserts the data into multiple tables. If I run the stored procedure into a database using a SSMS query window, everything works fine. However, we have a custom installation program that is used to deploy stored procedures to databases, and when this is used, execution of the sp fails with this error:

INSERT failed because the following SET options have incorrect settings:
'ANSI_NULLS, QUOTED_IDENTIFIER'. Verify that SET options are correct for use with
indexed views and/or indexes on computed columns and/or query notifications 
and/or xml data type methods.

The custom installation program does not use the correct settings when scripting in the stored procedures.

Setting these ( SET ARITHABORT ON; SET QUOTED_IDENTIFIER ON; SET ANSI_NULLS ON;) within the sp has no effect:

I have also tried setting these options for the open connection just before calling the sp in the code. This again does not have the desired effect.

It appears that the settings on the connection to the database while the sp is being run in to the database are what matters, not the settings when the sp is used.

I have experimented by playing with these settings in SSMS options, and this does appear to be the case. I would just like someone to confirm that this is definitely the case (if there is a way around I would love to hear it, but I'm not hopeful)

Unfortunately altering the installer program is not an option for me at the present time, so I'm looking at having to roll back a couple of weeks work; so if I do have to do this I want to be really sure (and have some evidence to back me up) that this is the only option

like image 512
DannykPowell Avatar asked Nov 11 '09 09:11

DannykPowell


People also ask

What is the difference between set quoted_identifier and off in SQL Server?

When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers.

What happens when we change the quoted_identifier option in SSMS tools?

Note: If we change the QUOTED_IDENTIFIER option SSMS tools, it turns off the QUOTED_IDENTIFIER for all client session. You should be careful about modifying this option in the production environment. We can use the SQL execution plan to view the current value of QUOTED_IDENTIFIER.

Should ANSI_nulls be set to on or IS NULL for distributed queries?

For a script to work as intended, regardless of the ANSI_NULLS database option or the setting of SET ANSI_NULLS, use IS NULL and IS NOT NULL in comparisons that might contain null values. ANSI_NULLS should be set to ON for executing distributed queries.

Why is the quoted identifier option stored as on in the metadata?

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.


1 Answers

The settings applied with those at CREATE or ALTER time and are ignored at runtime.

SSMS has correct settings by default (so does sqlcmd, osql etc).

From BOL, CREATE PROC, "Using SET Options"

The Database Engine saves the settings of both SET QUOTED_IDENTIFIER and SET ANSI_NULLS when a Transact-SQL stored procedure is created or modified. These original settings are used when the stored procedure is executed. Therefore, any client session settings for SET QUOTED_IDENTIFIER and SET ANSI_NULLS are ignored when the stored procedure is running. Other SET options, such as SET ARITHABORT, SET ANSI_WARNINGS, or SET ANSI_PADDINGS are not saved when a stored procedure is created or modified.

like image 97
gbn Avatar answered Oct 07 '22 08:10

gbn