|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 6:12 AM
Points: 2,526,
Visits: 3,620
|
|
Hm, the answer is kind of paradox.
Either the "the varchar is converted to int" or an "error occurs". But not both. I would have preferred an answer like this: An error occurs during conversion from varchar to int.
But i should mostly blame myself for not reading the answer to the end...
Best Regards,
Chris Büttner
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, June 10, 2009 10:50 PM
Points: 120,
Visits: 211
|
|
john.arnott (5/6/2008)
Excellent question in that to understand the result, you have to step through the entire process. --The statements compile. --Each "select" is executed. --Then the two result sets are combined to effect the Union. That's where the error occurs. That's why even though the alpha character "B" was earlier in the code, the execution tried to convert it to the higher precedence integer type to satisfy the column type forced by the later value "4". The compiler couldn't catch this as it won't look at the values, only the datatypes. If the character input had been a digit (in quotes), it would run. This works: select '3' union select 4
good explanation,
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
Hmm, getting late to this, but I'm having difficulty accepting the answer provided here. Here is the exact result I received on a SQL Server 2000 instance:
Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value 'B' to a column of data type int.
Note the Syntax error - does this not fly in the face of all the explanations thus far??
(or is this a SQL Server 2005-specific question / answer and I simply didn't read properly?)
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Tao,
I think the difference is just in the wording of the error message. My SQL 2000 instance gives: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value 'B' to a column of data type int. and my 2005 gives:Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'B' to data type int. Note that both versions call it Msg 245, implying that it's the same error and the text has been corrected/clarified in the newer version. It's not really a syntax error at all, but a conversion error at the time the UNION is attempted.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
ah-hah, thanks John, that makes a whole lot of sense!
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
good question with great discussion
|
|
|
|