Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of SET ANSI_WARNINGS OFF

Tags:

sql-server

if I have SET ANSI_WARNINGS OFF in one stored procedure to get rid of a warning, it only takes effects of that stored procedure and has no impact on other ones, which means in other stored procedures, the ANSI_WARNINGS is still on. What if I want to turn it off for all stored procedures? Why it is default on? How could I know that?

Do other settings(e.g., NOCOUNT) in sql server work the same way?

Thanks a lot.

It will be great if anybody can share articles about common characteristics of these settings with me.

like image 579
Ziyang Zhang Avatar asked Aug 08 '11 21:08

Ziyang Zhang


People also ask

What is the use of set Ansi_nulls on?

ANSI_NULLS should be set to ON for executing distributed queries. ANSI_NULLS must also be ON when you are creating or changing indexes on computed columns or indexed views. If SET ANSI_NULLS is OFF, any CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail.

Why we use set Nocount on in stored procedure?

SET NOCOUNT ON prevents the sending of DONEINPROC messages to the client for each statement in a stored procedure.

How do I suppress warnings in SQL?

To suppress warnings, set SQL_NOTES=0.

What is @@ options in SQL Server?

There is a variable in SQL Server called @@OPTIONS. This contains all of the settings that you have configured using the SET command. However, understanding the values inside of this variable can be a bit cumbersome. This article will show you a quick way to determine what settings you have in your environment.


2 Answers

It's possibly worth point out that not only does it NOT affect other stored procedures, it only affects statements following the SET operation

e.g. run this and you will see that the option is turned off then on and then off again.

CREATE TABLE test
(
    intvalue int NULL
) 
INSERT INTO test VALUES (1)
INSERT INTO test VALUES (NULL)
SET ANSI_WARNINGS OFF
SELECT COUNT(intvalue) FROM test
-- (1 row(s) affected)
SET ANSI_WARNINGS ON
SELECT COUNT(intvalue) FROM test
-- (1 row(s) affected)
-- Warning: Null value is eliminated by an aggregate or other SET operation.
SET ANSI_WARNINGS OFF
SELECT COUNT(intvalue) FROM test
-- (1 row(s) affected)
DROP TABLE test
like image 142
David Bridge Avatar answered Sep 30 '22 13:09

David Bridge


From BOL:

SQL Server includes the ANSI_WARNINGS database option. This is equivalent to SET ANSI_WARNINGS. When SET ANSI_WARNINGS is ON, errors or warnings are raised in divide-by-zero, string too large for database column, and other similar errors. When SET ANSI_WARNINGS is OFF, these errors and warnings are not raised. The default value in the model database for SET ANSI_WARNINGS is OFF. If not specified, the setting of ANSI_WARNINGS applies. If SET ANSI_WARNINGS is OFF, SQL Server uses the value of the is_ansi_warnings_on column in the sys.databases catalog view.

You can read about it in BOL (F1 in Management Studio) or on MSDN http://msdn.microsoft.com/en-us/library/ms190368.aspx

NOCOUNT works the same.

like image 41
Igor Borisenko Avatar answered Sep 30 '22 12:09

Igor Borisenko