Solved

Whats wrong with this SQL statement?

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now