Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 CASE in where statement

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2007-02-09 : 13:11:57
i have a SP like below... I am keep getting an error in the case statment: Incorrect syntax near '='.

what am i doing wrong?

alter proc [dbo].[usp_Cumulativeconsolidatedreport] ( @year int , @region int = 1000 , @month int )
AS

declare @prev_month int
declare @prev_year int
SET @prev_month = MONTH(DATEADD(month, @month , DATEADD(year, @year - 1900, 0)))
SET @prev_year = YEAR(DATEADD(month, @month , DATEADD(year, @year - 1900, 0)));



select sum(isnull(bad_dept_provision ,0) ) as badp ,a.month,c.region as Region_num,b.region_num as region, b.unit_name ,b.unit_abbr as unit_abbr, a.financial_class_code
FrOM [Revenue_Aggregate_Assertions] a
join Revenue_aggregate b on a.[month] = b.[month] and a.[year] = b.[year] and a.[Unit_abbr] = b.[Unit_abbr] and a.financial_class_code = b.financial_class_code
left join regions c on b.region_num = c.region_num
where
(
((b.month < @prev_month and b.year = @prev_year )
or
(b.year < @prev_year ))

)
and
case when @region= 1000 then (@region = 1000 or b.region_num = @region)
when @region = 998 then b.region_num in ( 5,6,7,8)
when @region = -1 then b.unit_abbr in ( 'B01','B02','B03','B04','B05','B06','B07')
when @region = 999 then b.unit_abbr not in ( 'B01','B02','B03','B04','B05','B06','B07')
else (b.region_num = @region)
end
group by a.month,c.region, b.unit_name ,c.region, b.region_num ,b.unit_abbr,a.financial_class_code) a



X002548
Not Just a Number

15586 Posts

Posted - 2007-02-09 : 13:19:53
Case needs to return a value in the codition, not set it



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -