Groups | Blog | Home
all groups > sql server programming > june 2006 >

sql server programming : How to use Case Statement


Tanweer
6/16/2006 4:22:01 PM
I tried and came up with no solution.
I need to use Stored procedure and no dynamic sql.
I ma using ms sql 2000 service pack 3

I am using three different tables. How I can make it to run union statements
only for those table where passing values for the variable is 1

Thanks
Tanweer


Declare @people_list int
Declare @client_list int
Declare @joborder_list int

set @people_list=1
set @client_list =0
set @joborder_list =1

select id, code form people_list
union
select id, code form Client_list
union
select id, code form joborder_list





Kalen Delaney
6/16/2006 4:43:10 PM
First of all, CASE is an expression, not a statement, so it has to be used
as a value inside a statement. There is probably some way to get a CASE
expression to help you hear, but you might try something like this instead:


Declare @people_list int
Declare @client_list int
Declare @joborder_list int

set @people_list=1
set @client_list =0
set @joborder_list =1

select id, code form people_list
where @people_list=1
union
select id, code form Client_list
where @client_list = 1
union
select id, code form joborder_list
where @joborder_list =1

So the where clauses will resolve to either WHERE 1=1, which will return all
the rows, or WHERE 0 =1 which will return none of the rows.

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

Tanweer
6/16/2006 5:08:01 PM
Love you guys

Great help

Thanks very much
Tanweer

[quoted text, click to view]
Dan Guzman
6/16/2006 6:41:55 PM
Try:

SELECT id, code form people_list
WHERE @people_list = 1
UNION
SELECT id, code form Client_list
WHERE @client_list = 1
UNION
SELECT id, code form joborder_list
WHERE @joborder_list =1

Also, consider using UNION ALL instead of UNION unless you need to remove
duplicate rows in the result.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Roy Harvey
6/16/2006 7:41:51 PM
select id, code form people_list
WHERE @people_list = 1
union
select id, code form Client_list
WHERE @client_list = 1
union
select id, code form joborder_list
WHERE @joborder_list = 1

Roy Harvey
Beacon Falls, CT

On Fri, 16 Jun 2006 16:22:01 -0700, Tanweer
[quoted text, click to view]
AddThis Social Bookmark Button