SA0091 : Setting the QUOTED_IDENTIFIERS or ANSI_NULLS options inside stored procedure, trigger or function will have no effect
Introduction
Section titled “Introduction”Setting the QUOTED_IDENTIFIERS or ANSI_NULLS option inside the body of stored procedures is ignored and will not be effective.
Description
Section titled “Description”When T-SQL developers configure QUOTED_IDENTIFIERS or ANSI_NULLS within the body of a stored procedure in SQL Server, it leads to ineffective settings.
For example:
-- Example of ineffective settingsCREATE PROCEDURE ExampleProcedureASBEGIN SET QUOTED_IDENTIFIER ON; SET ANSI_NULLS ON; SELECT * FROM TableName;END;Settings like QUOTED_IDENTIFIERS or ANSI_NULLS are ignored inside stored procedures, triggers, or functions, causing potential logical errors or incorrect results.
-
These settings affect how SQL Server interprets and executes certain T-SQL syntax, including how identifiers are quoted and how NULL values are compared.
-
Ignoring these options inside stored procedures can inadvertently cause different behavior than expected, leading to bugs and maintenance issues.
How to fix
Section titled “How to fix”This guidance addresses issues related to defining QUOTED_IDENTIFIERS or ANSI_NULLS settings within stored procedures, which can lead to ineffective settings and potential logical errors.
Follow these steps to address the issue:
1.Examine the stored procedure to locate any SET QUOTED_IDENTIFIER or SET ANSI_NULLS statements. These settings should not be placed inside the procedure body.
2.Remove the SET QUOTED_IDENTIFIER and SET ANSI_NULLS statements from within the stored procedure.
3.Set SET QUOTED_IDENTIFIER and SET ANSI_NULLS outside the stored procedure creation statement if needed, ensuring they are configured at the session or batch level before executing the procedure.
For example:
-- Prior to creating the procedure, set the necessary options at the session level:SET QUOTED_IDENTIFIER ON;SET ANSI_NULLS ON;
-- Create the stored procedure without these settings insideCREATE PROCEDURE ProperExampleProcedureASBEGIN SELECT * FROM TableName;END;The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”20 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”CREATE PROCEDURE test_proc AS
SET QUOTED_IDENTIFIER OFFSET ANSI_NULLS OFFSET ARITHABORT OFFSET ANSI_NULL_DFLT_ON OFFSET ANSI_DEFAULTS OFFSET ANSI_WARNINGS OFFSET ANSI_PADDING OFFSET CONCAT_NULL_YIELDS_NULL OFFSET NUMERIC_ROUNDABORT ONSET NOCOUNT ONSET ROWCOUNT 100SET XACT_ABORT ONSET IMPLICIT_TRANSACTIONS ONSET ARITHIGNORE OFFSET LOCK_TIMEOUT 1SET FMTONLY ONSET NOEXEC ONSET PARSEONLY OFF
SELECT au_lname, au_fname, au_id from authorsWHERE au_lname like 'L%'--Option (Keep Plan)Analysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0091 : Setting the QUOTED_IDENTIFIER option will have no effect when done inside stored procedure, trigger or function. | 3 | 4 |
| 2 | SA0091 : Setting the ANSI_NULLS option will have no effect when done inside stored procedure, trigger or function. | 4 | 4 |