Execution of CASE statement in SQL query -
let's have table named 'neighbors' contains variable 'name', , selecting name , using case statement assign values second variable, 'age'. syntax 1 can use case statement in sql takes 2 different forms:
1) simple case expression:
case name when 'george' 36 when 'alfred' 40 else null end age
2) searched case expression:
case when name = 'george' 36 when name = 'alfred' 40 else null end age
the 2 statements doing same thing. question is, how these statements handled in memory? handled same, or may 1 more efficient other?
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql
correct syntax either
case name when 'george' 36 when 'alfred' 40 else null end age
or
case when name = 'george' 36 when name = 'alfred' 40 else null end age
the benefit of latter allows multiple columns evaluated:
case when name = 'george' , othercolumn = 'stuff' 36 when 'alfred' 40 else null end age
Comments
Post a Comment