Case statement in a Stored Procedure

Just wondering if it is possible to use the case statement to determine what to execute in a stored procedure.

The code snippet shows an example of what I'm trying to do. The select statements are the same because I haven't finished them yet, so please ignore that, as they will eventually be different when I get them finished.
BEGIN
	CASE @SearchBy
	WHEN 'Out' THEN
		SELECT DISTINCT 
				Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
		FROM    Deployment INNER JOIN
				Event ON Deployment.Evnt_ID = Event.Evnt_ID
		WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND 
				(Deployment.CreatedOn > '1/1/2008')
		GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
		ORDER BY Event.Evnt_Start_Date
	WHEN 'Paid' THEN
		SELECT DISTINCT 
				Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
		FROM    Deployment INNER JOIN
				Event ON Deployment.Evnt_ID = Event.Evnt_ID
		WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND 
				(Deployment.CreatedOn > '1/1/2008')
		GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
		ORDER BY Event.Evnt_Start_Date
	ELSE	
		SELECT DISTINCT 
				Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
		FROM    Deployment INNER JOIN
				Event ON Deployment.Evnt_ID = Event.Evnt_ID
		WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND 
				(Deployment.CreatedOn > '1/1/2008')
		GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
		ORDER BY Event.Evnt_Start_Date
	END	
END
 
I get the following errors:
Msg 156, Level 15, State 1, Procedure SP_AdminWeb_DeploymentFees_Select, Line 20
Incorrect syntax near the keyword 'CASE'.
Msg 156, Level 15, State 1, Procedure SP_AdminWeb_DeploymentFees_Select, Line 30
Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Procedure SP_AdminWeb_DeploymentFees_Select, Line 39
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure SP_AdminWeb_DeploymentFees_Select, Line 49
Incorrect syntax near the keyword 'END'.

Open in new window

LVL 3
quanmacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
try this:

BEGIN
        IF @SearchBy = 'Out'
            BEGIN
                SELECT DISTINCT
                                Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
                FROM    Deployment INNER JOIN
                                Event ON Deployment.Evnt_ID = Event.Evnt_ID
                WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND
                                (Deployment.CreatedOn > '1/1/2008')
                GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
                ORDER BY Event.Evnt_Start_Date
        END
            IF @SearchBy =  'Paid'
            BEGIN
                SELECT DISTINCT
                                Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
                FROM    Deployment INNER JOIN
                                Event ON Deployment.Evnt_ID = Event.Evnt_ID
                WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND
                                (Deployment.CreatedOn > '1/1/2008')
                GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
                ORDER BY Event.Evnt_Start_Date
        END
            IF @SearchBy NOT IN('Paid', 'Out')
            BEGIN    
                SELECT DISTINCT
                                Deployment.Evnt_ID, Event.Evnt_Start_Date, MAX(Deployment.CreatedOn) AS CreatedOn
                FROM    Deployment INNER JOIN
                                Event ON Deployment.Evnt_ID = Event.Evnt_ID
                WHERE   (Deployment.Evnt_ID IS NOT NULL) AND (Event.Evnt_Status = 'Canceled') AND
                                (Deployment.CreatedOn > '1/1/2008')
                GROUP BY Deployment.Evnt_ID, Event.Evnt_Start_Date
                ORDER BY Event.Evnt_Start_Date
        END    
END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quanmacAuthor Commented:
Thanks chapmandew,

That is what I had originally planned to do, but I wanted to see if the Case statement would work.

I did some more reading on the Case statement and found out that it can only be used to return a value and not to be used to determine which statements to run. http://p2p.wrox.com/topic.asp?TOPIC_ID=43503
0
spratkeCommented:
Just curious, why wouldn't you use an If, Else If, Else rather then three if statements?
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

quanmacAuthor Commented:
There is no Else If in TSQL
0
spratkeCommented:
That makes sense, I just saw this in an MS SQL thread.
0
spratkeCommented:
That makes sense, I just saw this in an MS SQL thread.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.