Solved

Case statement in a Stored Proc syntax

Posted on 2011-03-24
3
284 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 250 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 250 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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