|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, July 17, 2008 10:03 AM
Points: 1,
Visits: 20
|
|
| I agree that this doesn't work. I was pretty sure it wasn't going to do an implicit conversion. Then I cut and pasted it into a query and executed it. And I was proven right. It failed. No results were returned. An error was returned. It things like this that makes me not try to read or follow information from sites like this one. Is there a setting in MSSQL that allows implicit conversion? I hope not. My databases need to maintain data integrity, and if implicit conversions are happening I can't trust my databases.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
bvesperman (5/5/2008) I agree that this doesn't work. I was pretty sure it wasn't going to do an implicit conversion. Then I cut and pasted it into a query and executed it. And I was proven right. It failed. No results were returned. An error was returned. It things like this that makes me not try to read or follow information from sites like this one. Is there a setting in MSSQL that allows implicit conversion? I hope not. My databases need to maintain data integrity, and if implicit conversions are happening I can't trust my databases.
Yes, implicit conversions happen.
It's not a problem, if you know how they work. Books Online has exact data on what they do and why and how.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 4:46 PM
Points: 31,433,
Visits: 13,745
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 6:46 AM
Points: 5,102,
Visits: 20,207
|
|
| AS previously stated: dunnjoe (5/5/2008) That's why I feel answer b is not correct. Answer b says the value is converted to an int. The error message says the conversion failed. So, it appears to be a compilation error.
Unfortunately I came to the same conclusion ... the statement did NOT execute ... other than the error message
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'B' to data type int.
There were NO values returned!!
Still a good QOD just a bad wording of possible answers ... to the detriment of many of us
If everything seems to be going well, you have obviously overlooked something.
Ron
Please help us, help you -before posting a question please read Before posting a performance problem please read
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 6:46 AM
Points: 5,102,
Visits: 20,207
|
|
Steve Jones - Editor (5/5/2008) That is not a compilation error. It's a run-time error when a conversion occurs. There are very few compilation errors, mostly syntax issues.
In my humble opinion the answer The varchar is converted in int (error results) should have been
The varchar is not converted into int (run time error results)
Since this union statement will work (no run time error)
select '10' Union Select 4
and this will compile
CREATE PROCEDURE Dbo.Oops AS select 'B' Union Select 4
but will result in the run time error when invoked.
Oh well looking back now .... I have gained knowledge from the QOD and that is its objective.
Thanks Steve for making think even more
If everything seems to be going well, you have obviously overlooked something.
Ron
Please help us, help you -before posting a question please read Before posting a performance problem please read
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, April 15, 2010 9:38 AM
Points: 12,
Visits: 26
|
|
This will not compile in SQL 2005....
Conversion failed when converting the varchar value 'B' to data type int.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Today @ 11:20 AM
Points: 213,
Visits: 376
|
|
Trace Herrell (5/5/2008) This will not compile in SQL 2005....
Conversion failed when converting the varchar value 'B' to data type int.
On the contrary, it did compile but it encountered an error when it ran. If you mistyped and put in "Skelect 'B' ..." it would not compile. Conversion failure is a runtime error, in this case because a varchar 'B' can't be converted to int. The compiler doesn't check to make sure you won't run into data type errors.
Puto me cogitare, ergo puto me esse. I think that I think, therefore I think that I am.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, July 05, 2011 11:41 AM
Points: 45,
Visits: 173
|
|
I feel the same way, in addition note the wording:
This is an example of an implicit conversion. In this case, an error is returned as the varchar is converted to an int, or an attempt is made, which returns an error. The int is of higher precedence than a varchar, so that is the order of conversions.
What is it converted and when error says failed to convert and then you say varchar IS converted to an int and then you say OR attempt is made?
Which one is it?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 2,465,
Visits: 3,588
|
|
That will teach me for speed reading the answers - missed the (error results) at the end of answer two! So picked answer 3! My bad... (But at least I knew it wouldn't work!)
Rodney
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
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
|
|
|
|