[dba-SQLServer] If or Case or 'what'?

Stuart McLachlan stuart at lexacorp.com.pg
Tue Jun 20 17:13:44 CDT 2006


On 20 Jun 2006 at 18:03, Mark A Matte wrote:

> Thanks Again Stuart,
> 
> In this usage...what are the differences/advantages of using CASE vs IF?

Same as VB/VBA.

Once a case situation is matched, execution drops through to the "End"
With a string of simple "IF"s, every conditional has to be evaulated.

On average, you  will only have to evaluate half as many When"s as you will 
"If"s.   By putting the most likely case(s) first, you  can dramatically 
speed up execution of "Case" constructs.

Using If.. Else... has the same overhead as Case, but 30 Cases is a llot 
cleaner to code than 30 nested Ifs :-)

-- 
Stuart





More information about the dba-SQLServer mailing list