?
Solved

Whats wrong with this SQL statement?

Posted on 2009-05-06
2
Medium Priority
?
205 Views
Last Modified: 2012-05-06
I am trying to use a CASE statement in a WHERE clause, I have it to this point but when I try this I get the error "Incorrect syntax near the keyword 'END'".  And if I remove the single quotes surrounding the (companid=0....) I get a message saying the '=' is wrong.

I'm stuck... lol

p.s. This is the code in a stored procedure and all the variables are declared and correct.
BEGIN
	SELECT  
		count (DISTINCT (dbo.Member.ID)) AS mcount
	FROM 
		dbo.Member
	WHERE
		((SELECT count(billingid) FROM billingdata 
		  WHERE ((billingmonth>=@MONTH AND billingyr=@YEAR-1) OR (billingmonth<@MONTH AND billingyr=@YEAR)) AND (billingid=dbo.member.id)) >= 1
		) AND
		member.memberstatus=101 AND
		CASE
			WHEN @mtype='M' THEN '(companyid=0 AND departmentid=0 AND householdid=0)'
			WHEN @mtype='J' THEN '(companyid=0 AND departmentid=0 AND householdid>0)'
			WHEN @mtype='C' THEN '(companyid>0 AND departmentid=0 AND householdid=0)'
			WHEN @mtype='D' THEN '(companyid>0 AND departmentid>0 AND householdid=0)'
		END
END

Open in new window

0
Comment
Question by:StuartMc77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 260 total points
ID: 24312891
it's wrong :)
BEGIN
      SELECT  
            count (DISTINCT (dbo.Member.ID)) AS mcount
      FROM 
            dbo.Member
      WHERE
            ((SELECT count(billingid) FROM billingdata 
              WHERE ((billingmonth>=@MONTH AND billingyr=@YEAR-1) OR (billingmonth<@MONTH AND billingyr=@YEAR)) AND (billingid=dbo.member.id)) >= 1
            ) 
       AND member.memberstatus=101 
       AND (  (@mtype='M'  AND companyid=0 AND departmentid=0 AND householdid=0)
           OR (@mtype='J'  AND companyid=0 AND departmentid=0 AND householdid>0)
           OR (@mtype='C'  AND companyid>0 AND departmentid=0 AND householdid=0)
           OR (@mtype='D'  AND companyid>0 AND departmentid>0 AND householdid=0)
           )
           
END

Open in new window

0
 
LVL 1

Author Closing Comment

by:StuartMc77
ID: 31578383
LMAO... that makes so much sense... thank you very much... sorry for the low points, :o( I'm still trying to build them up
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question