Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Where clause Case Statement and IN


Where clause Case Statement and IN

Author
Message
Faye Fouladi
Faye Fouladi
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 187
How do I correct the following statement:

Declare @Component smallint = -1

Select srcComment.Comment As Comment, t.NameFull As CommentType, parenttypeId As Component,
(select Quarter From dimPeriod where periodId = srcComment.ParentId) As Quarter,
(select Year From dimPeriod where periodId = srcComment.ParentId) As periodYear
From srcComment
inner JOIN dimPeriod p
ON srcComment.ParentId = p.periodId
Left Outer Join refType t
ON t.TypeId = srcComment.ParentTypeId
where case @Component
when -1 then
parenttypeId IN (601, 605, 606)
else parenttypeId = @Component End
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24217 Visits: 37978
Faye Fouladi (12/24/2012)
How do I correct the following statement:

Declare @Component smallint = -1

Select srcComment.Comment As Comment, t.NameFull As CommentType, parenttypeId As Component,
(select Quarter From dimPeriod where periodId = srcComment.ParentId) As Quarter,
(select Year From dimPeriod where periodId = srcComment.ParentId) As periodYear
From srcComment
inner JOIN dimPeriod p
ON srcComment.ParentId = p.periodId
Left Outer Join refType t
ON t.TypeId = srcComment.ParentTypeId
where case @Component
when -1 then
parenttypeId IN (601, 605, 606)
else parenttypeId = @Component End


Hard to say, you tell us the error message you received.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Faye Fouladi
Faye Fouladi
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 187
I basically want to say if @Component = -1 then parentTypeId In (601, 605, 606) else parentTypeId = @Component
Faye Fouladi
Faye Fouladi
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 187
I get an error message near = on last line
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24217 Visits: 37978
I would do it something like this:



Declare @Component smallint = -1

Select
sc.Comment As Comment,
t.NameFull As CommentType,
sc.parenttypeId As Component,
dt.periodQuarter,
dt.periodYear
From
srcComment sc
inner JOIN dimPeriod p
ON srcComment.ParentId = p.periodId
Left Outer Join refType t
ON t.TypeId = srcComment.ParentTypeId
outer apply (select Quarter, Year from dimPeriod dp where dp.periodId = sc.ParentId)dt(periodQuarter, periodYear)
where
(@Component = -1 and sc.parenttypeId IN (601, 605, 606)) or
(sc.parenttypeId = @Component);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Faye Fouladi
Faye Fouladi
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 187
Thank you, it worked.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4259 Visits: 6431
Lynn has pointed you at a catch-all query that will work as you have indicated. But you should also read this article SQL in the Wild - Catch All Queries by Gail Shaw in case this method causes you performance issues.

I use catch-all queries myself in the same way that Lynn recommended, so this post is by no means a slight on his suggestion.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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