01.Blogs :
clintz  

T-SQL: coalesce

instead of using mulriple if's like:

if @user = ''
 begin
  if @age = ''
   begin
    select * from customers
   end
  else
   begin
    select * from customers where age = @age
   end
 end
else
 begin
  if @age = ''
   begin
    select * from customers where [name] = @user
   end
  else
   begin
    select * from customers where [name] = @user and age = @age
   end
 end

you can use the COALESCE() like:

if @user = ''
 set @user = null
if @age = ''
 set @age = null

select * from customers where [name] = coalesce(@user, name) and age = coalesce(@age, age)

 

posted on Thursday, November 24, 2005 8:48 PM by clintz


 
03.UPDATE CALENDAR :
<November 2005>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR