|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 8:31 AM
Points: 1,146,
Visits: 1,448
|
|
Thanks for a Monday question, that still made me learn something.
Please don't go. The drones need you. They look up to you.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 7:02 AM
Points: 300,
Visits: 307
|
|
| Good introduction the CHOOSE function.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 2:52 PM
Points: 1,350,
Visits: 870
|
|
Great question about a new feature.
Oh no, we're toast! I've got this. *Keyboard clatter* Woah, how'd you do that? I'm a DBA...Booyah
Yeah, uh huh, you know what it is. Everything I do, I do it big

|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 9:52 AM
Points: 1,356,
Visits: 4,761
|
|
What would be a good use of this function? Why wouldn't you just add a lookup table containing the index and description, and join to that? So that any changes required to a description could be made by just updating the lookup table, rather than rewriting SQL.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, April 19, 2013 10:27 AM
Points: 690,
Visits: 1,100
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 3,226,
Visits: 64,072
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 8:39 AM
Points: 1,045,
Visits: 218
|
|
| Thanks, for the reminder about the Choose function. The beauty is the ability to provide two things, an alternate name or identification as well as a different sort order than if the value provided was the content of the field. Of course this really only works well if there is a short list of values to translate.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, April 19, 2013 10:27 AM
Points: 690,
Visits: 1,100
|
|
Toreador (1/14/2013) What would be a good use of this function? Why wouldn't you just add a lookup table containing the index and description, and join to that? So that any changes required to a description could be made by just updating the lookup table, rather than rewriting SQL.
This was exactly what I was wondering.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Today @ 8:13 AM
Points: 721,
Visits: 1,369
|
|
SQLRNNR (1/12/2013) Glad we have this feature added.
But are the CHOOSE function and its also-new-to-SQL-Server-2012 buddy IIF anything other than syntactical sugar for Visual Basic coders writing T-SQL? The CHOOSE function is just a less-flexible shorthand for the ANSI-standard CASE, and it can only be used when evaluating an equality condition for an integer value (the index argument), although I concede that CHOOSE requires less typing than CASE in these limited circumstances. Similarly, the IIF function saves a few keystrokes when there are only two possible values to return, but once you start nesting IIF statements, the code becomes more difficult to understand than the equivalent CASE expression, in my opinion.
SQL Server 2012 just converts CHOOSE and IIF function calls to CASE expressions anyway. I have a code sample that shows that the CHOOSE and IIF functions produce Compute Scalar operators that have Defined Values that are identical to the one produced by the equivalent CASE expression, but I'm having some trouble posting it. I'll try again later.
Edit: I have posted a screenshot of the code, a .txt file of the code, and a .sqlplan file of the execution plan that I get on my SQL Server 2012 instance, if you're interested. Still not sure why I can't post code - I get an error screen that suggests that my employer is blocking the outgoing traffic when it includes certain kinds of code snippets - hmmm . . .
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 4:24 AM
Points: 1,158,
Visits: 642
|
|
L' Eomot Inversé (1/12/2013)
Nice clear straightforward question. Good to see a question on this new feature. Interestingly, there's an error on the BoL page: it says the CHOOSE function BoL Returns the data type with the highest precedence from the set of types passed to the functionThe first argument is an integer, which has a higher type precedence than any character type, but if it really was going to return an int we would have string to int conversion errors here. BoL should instead say that the return type is highest precedence type of the arguments other than the first.
I read this and changed my mind to integer. Ah well, another point gone begging.
|
|
|
|