Solved

Whats wrong with this SQL statement?

Posted on 2009-05-06
2
201 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
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 65 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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