SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CASE IN in where clause


CASE IN in where clause

Author
Message
aldoc
aldoc
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 8
I am trying to select different data from a table depending on what the passed parameter value is. If the parameter is 0 then select all products from plu_file_dates with ToDept value of 60 in the plu_file or else select all products with ToDept of 60 and FromDept with value matching parameter.
However no matter how I try to write the where statement it always fails to execute

DECLARE @Dept AS INT
SET @Dept = 40

SELECT * FROM plu_file_dates
WHERE pluid in (SELECT pluid FROM plu_file WHERE (CASE WHEN @Dept = 0 THEN (ToDept = 60) ELSE (FromDept = @Dept and ToDept = 60) END))
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8800 Visits: 8490
You can fix the syntax like this: -
DECLARE @Dept AS INT
SET @Dept = 40

SELECT *
FROM plu_file_dates
WHERE pluid IN (SELECT pluid
FROM plu_file
WHERE ToDept = 60 AND FromDept = CASE WHEN @Dept = 0
THEN FromDept
ELSE @Dept END
);



However, this isn't the best way for the query optimiser to work, see Gail's blog post here --> http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

Instead, do something like this: -
DECLARE @Dept AS INT;
SET @Dept = 40;

IF @Dept = 0
BEGIN
SELECT *
FROM plu_file_dates
WHERE pluid IN (SELECT pluid
FROM plu_file
WHERE ToDept = 60
);
END
ELSE BEGIN
SELECT *
FROM plu_file_dates
WHERE pluid IN (SELECT pluid
FROM plu_file
WHERE ToDept = 60 AND FromDept = @Dept
);
END




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
jeetsingh.cs
jeetsingh.cs
Mr or Mrs. 500
Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)Mr or Mrs. 500 (557 reputation)

Group: General Forum Members
Points: 557 Visits: 618
it think the way you have written the query is not right
case statement should be the part of the expression
in your case you can use case like this
select * from sys.objects
where name=(case when name='t1 ' then 'condition '
else 'condition2'
end)
aldoc
aldoc
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 8
Thanks, used the second solution and works perfectly
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search