Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Stored Procedure SET Options

What are the best SET Options to issue before stored procedure creation?

for example

SET QUOTED_IDENTIFIER  OFF
SET ANSI_NULLS  ON 

CREATE PROCEDURE HelloWorld
AS
    --also, should any be issued within the procedure body?
    PRINT 'hello world!'
    RETURN 0
GO

By best, I mean the most preferred settings.

like image 702
RacerX Avatar asked Sep 15 '25 08:09

RacerX


2 Answers

For stored procedure creation specifically, then only two are important at parse time

SET ANSI_NULLS
SET QUOTED_IDENTIFIER

And they should be ON to work with newer SQL Server functionality that only works these being ON

Stored procedures execute with the SET settings specified at execute time except for SET ANSI_NULLS and SET QUOTED_IDENTIFIER. Stored procedures specifying SET ANSI_NULLS or SET QUOTED_IDENTIFIER use the setting specified at stored procedure creation time. If used inside a stored procedure, any SET setting is ignored.

Ref: http://msdn.microsoft.com/en-us/library/ms190356.aspx

To set both at the same time and others as well, use

SET ANSI_DEFAULTS ON

When enabled (ON), this option enables the following ISO settings:

SET ANSI_NULLS
SET CURSOR_CLOSE_ON_COMMIT
SET ANSI_NULL_DFLT_ON
SET IMPLICIT_TRANSACTIONS
SET ANSI_PADDING
SET QUOTED_IDENTIFIER
SET ANSI_WARNINGS

For completeness, set these other three at the connection level which allows things like INDEXED VIEWs to work correctly

SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET NUMERIC_ROUNDABORT OFF
like image 123
RichardTheKiwi Avatar answered Sep 17 '25 01:09

RichardTheKiwi


Transaction Isolation Level (often Read committed - to ensure no dirty reads). And unless you're interested in the number of rows affected; set nocount on

like image 30
DaveShaw Avatar answered Sep 16 '25 23:09

DaveShaw