Logical Functions – CHOOSE()

I am going write a couple of tidbits on Transaction SQL logical functions.

These functions were introduced with the SQL Server 2012 release. Both functions have their origins from Visual Basic for Applications (VBA) framework.

I think of these function as decision trees. Especially when they are nested to several levels.

If you have a kid like I do, you might find the decision tree for dropped food funny. Please see image to left.

I will be exploring the CHOOSE function today.

If you grew up like I did, you probably saw black and white re-runs of the Three Stooges on television.

The example below uses the RAND function, to get a random index value. The index value is used with the CHOOSE() function to return the culprit who broke our chair.

Many chairs were broken in the Three Stooges slapstick skits.

The output of each of the five batches is listed below.

The function takes an index value N and a list of items. It returns the Nth item in the list.

Please note the number five after the key word GO. This tells the transaction processor to run the commands (batch) five times.

Next time, I will be talking about the immediate if, IIF function.

Related posts

Leave a Comment