?
Solved

Case statement in a Stored Proc syntax

Posted on 2011-03-24
3
Medium Priority
?
287 Views
Last Modified: 2012-06-21
Can someone please point out the errors in my syntax here..Basically just going to call this proc from SSRS in order to bring back one of four datasets and the 'sitename' if that possible...

Thanks in Advance,Gray

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Author,,Name>
-- Create date: <Create Date,,>
-- Description:      <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Digital_Sitename]      
                                                @start datetime,
                                                @end datetime,
                                                @varSitename nvarchar(40)
AS
BEGIN

      SET NOCOUNT ON;

                  SELECT Case
                                    WHEN @varSitename = 'Google' THEN
                                          
                                          SELECT sitename,campaign,SUM(cost)as 'sitecost',SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename = 'Google'
                                          group by sitename,campaign
                                          
                                    When @varSitename='Bing' THEN
                                    
                                          SELECT sitename,campaign,SUM(cost)as 'sitecost',SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename = 'Bing'
                                          group by sitename,campaign

                                    WHEN @varSitename='Marchex' THEN
                                    
                                          SELECT sitename,campaign,(SUM(calls)*15) as 'sitecost', SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename ='Marchex'
                                          group by sitename,campaign

                                    WHEN @varSitename='Yellow Pages' THEN
                                    
                                          Select sitename,campaign, (DATEDIFF(D,@start,@end)+1)*(8774/30) as 'spend',sum(calls)as 'calls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end) and sitename ='Yellow Pages'
                                          group by sitename,campaign

                  END Case
GO      
END



0
Comment
Question by:Gray5452
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Assisted Solution

by:techhealth
techhealth earned 1000 total points
ID: 35208558
Case can't be used to control execution.  It only supplies values to expressions.  You'll have to use IF... THEN to control flow.
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 total points
ID: 35208571
USE IF/ELSE
CREATE PROCEDURE [dbo].[Digital_Sitename]      
                                                @start datetime, 
                                                @end datetime, 
                                                @varSitename nvarchar(40)
AS
BEGIN

      SET NOCOUNT ON;

                  
                                    IF @varSitename = 'Google' 
                                    BEGIN      
                                          SELECT sitename,campaign,SUM(cost)as 'sitecost',SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename = 'Google'
                                          group by sitename,campaign
                                    END      
                                    ELSE IF @varSitename='Bing'  
                                    BEGIN
                                          SELECT sitename,campaign,SUM(cost)as 'sitecost',SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename = 'Bing'
                                          group by sitename,campaign
									END
                                    ELSE IF @varSitename='Marchex' 
                                    BEGIN
                                          SELECT sitename,campaign,(SUM(calls)*15) as 'sitecost', SUM(calls)as 'sitecalls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end)and sitename ='Marchex'
                                          group by sitename,campaign
									END
                                    ELSE IF @varSitename='Yellow Pages' 
                                    BEGIN
                                          Select sitename,campaign, (DATEDIFF(D,@start,@end)+1)*(8774/30) as 'spend',sum(calls)as 'calls'
                                          FROM [KoolSmiles].[dbo].[v_DigitalMedia]
                                          where (REportDate between @start and @end) and sitename ='Yellow Pages'
                                          group by sitename,campaign
                                    END

                  
 
END

Open in new window

0
 

Author Closing Comment

by:Gray5452
ID: 35208637
Perfect,,,Thanks tons,,,that was fast too,,,you guys are good
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

801 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