|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 4:00 PM
Points: 5,102,
Visits: 20,205
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Sunday, May 05, 2013 11:42 AM
Points: 374,
Visits: 326
|
|
thanks for the question.
-------------------------------------------------------------------------------------- Hai Ton My DB4BREAKFAST blog: http://db4breakfast.blogspot.com
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 3:07 PM
Points: 7,096,
Visits: 7,156
|
|
Nice clear and unambiguous question. Back to basics, really. Possibly difficult for people who are used to working with ANSI WARNINGS ON, but pretty basic for the rest of the world.
The explanation is a bit incomplete though. The reference is to the right BOL page, but the text quoted from that page is actually irrelevant because it's saying that even when ANSI_WARNINGS is set to ON some statements (parameter passing to SP or to UDF, assignment to variables) don't follow the ansi standard for treating overflow and zero divide errors that occur in INSERT or UPDATE statements - actually I can't understand why anyone would think it might or should, but there's no harm in making it clear. It says nothing at all about what happens when ANSI_WARNINGS is set to OFF and that's what the question is about. There's no text at all in the explanation that describes what happens when ANSI_WARNINGS is OFF.
In fact some of the other text on the BOL page is a bit of a mess:
Bol When set to ON, the divide-by-zero and arithmetic overflow errors cause the statement to be rolled back and an error message is generated. When set to OFF, the divide-by-zero and arithmetic overflow errors cause null values to be returned. The behavior in which a divide-by-zero or arithmetic overflow error causes null values to be returned occurs if an INSERT or UPDATE is tried on a character, Unicode, or binary column in which the length of a new value exceeds the maximum size of the column. If ANSI_WARNINGS is ON, the INSERT or UPDATE is canceled as specified by the ISO standard. Trailing blanks are ignored for character columns and trailing nulls are ignored for binary columns. When OFF, data is truncated to the size of the column and the statement succeeds. The third sentence appears to say that NULL values occur when zero divide or aithmetic overflow errors occur and ANSI_WARNINGS is OFF, and that this includes the case when INSERT or UPDATE tries to stuff more into a character or unicode column than will fit. However, this is contradicted by the sixth sentence, so perhaps the third sentence applies when ANSI_WARNINGS is ON? But that would contradict the first sentence! The obvious correction is to get rids of the third sentence and make teh fourth sentence say that it applies to putting overlength data into character, unicode, or binary string columns (ie grab the last bit of the third sentenbce and put it into the fourth sentence).
It isn't all that unusual for a BOL page to be a bit of a mess, but it usually gets fixed when it's pointed out. This time MS has left it a mess for a decade or more after it was pointed out to them (the offending text dates from BOL for SQL 2000, or perhaps even earlier).
edit: spelling
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, May 18, 2013 6:46 PM
Points: 1,074,
Visits: 1,076
|
|
nice and easy Q for the monday ...
and a great explanation by Tom ...
thanks for Q & E
~ demonfox ___________________________________________________________________ Wondering what I would do next , when I am done with this one
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 1:02 AM
Points: 1,174,
Visits: 1,250
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:32 AM
Points: 1,083,
Visits: 1,158
|
|
I got the correct answer but chose wrong and lose easy point... but thanks it's nice question and good start of week...
Keep Learning - Keep Growing !!! http://growwithsql.blogspot.in
Thanks Vinay Kumar
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
good basic question to start the week  +1
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 1:21 AM
Points: 9,374,
Visits: 6,471
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:52 AM
Points: 2,476,
Visits: 2,137
|
|
Nice and gentle start to the week - thanks
------------------------------- Posting Data Etiquette - Jeff Moden Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:33 AM
Points: 5,241,
Visits: 7,049
|
|
Like Tom, I too think the explanation in Books Online is very unsatisfactory. So I did some tests to check a few of the issues.
In this specific question, two things are at stake: truncatiing a string (both on assignment to variable and during insertion in a table) and implicit truncating to integer.
String truncation: With ANSI_WARNINGS OFF, this is always accepted, with no error or warning message. With ANSI_WARNINGS ON, truncation on assignment to a variable is still accepted, but truncating when inserting to the table generates an error message and causes the statement to fail. Changing the ANSI_WARNINGS to ON in the query posted in the question will result in this error message, and the SELECT runs and shows only the column headers (as there is no row inserted).
Numeric truncation: Truncating the number as part of the conversion to integer is always allowed and will never generate a warning or error, regardless of the ANSI_WARNINGS setting.
According to Books Online, two other issues to beware of are division by zero and arithmetic overflow.
Division by zero: Any attempt to divide by zero will always cause an error. With ANSI_WARNINGS ON, this aborts the statement, but allows the rest of the batch to continue. So if the error takes place in the SET, the SET will not be executed (variable is still NULL), and the INSERT executes (inserting a NULL value). If the error is in the INSERT itself, no row is inserted.
Arithmetic overflow: Same behaviour as division by zero.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|