[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Case statement in a Stored Procedure

Posted on 2008-11-18
6
Medium Priority
?
8,088 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:quanmac
  • 3
  • 2
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 1500 total points
ID: 22989182
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
 
LVL 3

Author Comment

by:quanmac
ID: 22989405
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
 

Expert Comment

by:spratke
ID: 25355955
Just curious, why wouldn't you use an If, Else If, Else rather then three if statements?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 3

Author Comment

by:quanmac
ID: 25356395
There is no Else If in TSQL
0
 

Expert Comment

by:spratke
ID: 25360811
That makes sense, I just saw this in an MS SQL thread.
0
 

Expert Comment

by:spratke
ID: 25360812
That makes sense, I just saw this in an MS SQL thread.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

829 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