December 24, 2012 at 1:04 pm
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
December 24, 2012 at 1:05 pm
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.
December 24, 2012 at 1:07 pm
I basically want to say if @Component = -1 then parentTypeId In (601, 605, 606) else parentTypeId = @Component
December 24, 2012 at 1:08 pm
I get an error message near = on last line
December 24, 2012 at 1:12 pm
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);
December 24, 2012 at 1:18 pm
Thank you, it worked.
December 24, 2012 at 6:25 pm
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[/url] 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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply