Home All Groups Group Topic Archive Search About

SET ANSI_WARNINGS OFF: can this be table-specific?

Author
14 Jul 2006 3:07 PM
thatsMaBoy
Hi,

I am using SQL Server 2000.  I know that it is bad practice to truncate
data as it is lost but please could someone answer the following:

1. Can ANSI_WARNINGS be set to OFF on a table-specific basis, or is it
database-specific?  Basically, I want to be able to INSERT/UPDATE
certain tables by setting ANSI_WARNINGS to OFF just before the
INSERT/UPDATE then set it back to ON straight afterwards.  The reason
being, other users will be inserting/updating other tables at
potentially the same time as me and I do not want truncating to be
allowed on those tables.

2. I am using ASP/VBScript to insert/update the database.  Do I need to
simply include the words "SET ANSI_WARNINGS OFF" as part of the SQL
string that I execute, or do I need to execute this command first, then
execute the insert/update second?

3. If I set the value to OFF, will it remain OFF until someone sets it
back to ON?

Many thanks.

Jimmy

Author
14 Jul 2006 10:30 PM
Erland Sommarskog
thatsMaBoy (jimmyfishb***@yahoo.co.uk) writes:
> 1. Can ANSI_WARNINGS be set to OFF on a table-specific basis, or is it
> database-specific? 

It's session-specific.

> Basically, I want to be able to INSERT/UPDATE certain tables by setting
> ANSI_WARNINGS to OFF just before the INSERT/UPDATE then set it back to
> ON straight afterwards.  The reason being, other users will be
> inserting/updating other tables at potentially the same time as me and I
> do not want truncating to be allowed on those tables.

You can use SET ANSI_WARNINGS OFF to turn off warnings temporarily.
However, beware that if you do this in a query batch, the batch will
be recompile when the setting changes. The same applies if you use
the command in a stored procedure.

> 2. I am using ASP/VBScript to insert/update the database.  Do I need to
> simply include the words "SET ANSI_WARNINGS OFF" as part of the SQL
> string that I execute, or do I need to execute this command first, then
> execute the insert/update second?

You don't need to have a seprate batch, but from what I said above, it's
better to send it separately for the sake of performance.

> 3. If I set the value to OFF, will it remain OFF until someone sets it
> back to ON?

The effect of SET options lasts until the end of the scope. That is,
if you issue it in a stored procedure, the effect is reverted when
the procedure exits. If you issue it directly from the client, the
effect will last until you disconnect or issue a new SET command.


All this said: how difficult is to use substring to ensure that
truncation does not happen? Then you don't need to fiddle with
ANSI_WARNINGS off.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button