Solved

Case statement in a Stored Proc syntax

Posted on 2011-03-24
3
276 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
3 Comments
 
LVL 11

Assisted Solution

by:techhealth
techhealth earned 250 total points
Comment Utility
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:
ewangoya earned 250 total points
Comment Utility
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
Comment Utility
Perfect,,,Thanks tons,,,that was fast too,,,you guys are good
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now