Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Case statement in a Stored Proc syntax

Posted on 2011-03-24
3
Medium Priority
?
288 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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