简体   繁体   中英

How to change ANSI_NULLS remotely for a procedure or migrate procedure with ANSI_NULLS set OFF

I know there are numerous topics regarding migrating or syncing stored procedures to another server, anyhow, I'm not able to find out some acceptable answer to following question.

I want to migrate stored procedure with ANSI_NULL value set to OFF by a script. Example of the procedure I want to migrate:

USE [myDB]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[p_test_ansi_nulls] AS
SELECT CASE WHEN NULL = NULL THEN 'haha' ELSE 'no-haha' END h

ANSI_NULLS value is stored in sys.sql_modules table, but not in the procedure itself. I tried to create statement with the header (with SET ANSI_NULLS OFF string included) and execute via sp_executesql, but got an error:

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

So is there any way, how to migrate procedure to another server with this option or way how to change this setting remotely? (procedure is quite old and complex, therefore we can't change the code..)

Do it exactly the way you wrote up there.

SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[p_test_ansi_nulls] AS
SELECT CASE WHEN NULL = NULL THEN 'haha' ELSE 'no-haha' END h

This will create procedure with ANSI_NULLS off:

SQLFiddle DEMO

Also, pease note - from msdn: http://msdn.microsoft.com/en-us/library/ms188048.aspx

In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM