Solved

Whats wrong with this SQL statement?

Posted on 2009-05-06
2
202 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 143

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query 8 51
SQL Insert parts by customer 12 42
Help Required 3 106
Syntax for query to update table 2 29
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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