Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:58:58+00:00 2026-06-11T02:58:58+00:00

I’ve pieced together this code to create a pivot on a sum of invoice

  • 0

I’ve pieced together this code to create a pivot on a sum of invoice subtotals per a dealership. It works, and produces the correct results. However, when I try to convert it to a stored procedure the infamous squiggly red line appears under “CREATE VIEW Revenues” in SQL Server management studio and the error says:

‘CREATE VIEW must be the only statement in the batch’ and I don’t know enough T-SQL to proceed as I need to be able to execute it from a ASP.NET/C# web app.

I’m trying create an SP by putting:

1)

USE [Sherwood]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[usp_GetRevenues] 

@StartDate AS DateTime = '20120301',
@EndDate AS DateTime = '20120401'
AS
SET NOCOUNT ON;

BEGIN

in front, with an END statement at the end.

2) The parameters are necessary to be able to filter the data for a period of months, but I appear not to be able to pass parameters or use variables with the code as is.

Thanks in advance.

Code as is below – showing where I need to filter with variables. The code works only with dates in the format ‘1/1/2012’ in the where clause, but need to be able to pass variables as shown.

–================================

CREATE VIEW Revenues
AS
SELECT  
i.DateOrdered
,LTRIM(STR(DATEPART(MONTH,i.DateOrdered))) AS [Month]  
,LTRIM(STR(YEAR(i.Dateordered))) AS [Year]  
,c.CustomerCode
,SUM(i.Jobprice) AS Subtotal 
FROM Invoices i
JOIN
Customers c
ON i.CustomerID = c.ID
WHERE i.DateOrdered >= @StartDate 
AND i.DateOrdered <= @EndDate **<-- and here.**  
GROUP BY ROLLUP (YEAR(i.DateOrdered), 
MONTH(i.DateOrdered), i.DateOrdered, c.CustomerCode);
GO

IF OBJECT_ID('Revenues2') IS NOT NULL
DROP VIEW Revenues2
GO

CREATE VIEW Revenues2
AS
SELECT 
 LTRIM(STR([YEAR])) + '-' + STUFF([Month],1,0, REPLICATE('0', 2 - LEN([Month]))) 
 AS [Date] 
,Subtotal
,CustomerCode
FROM Revenues
WHERE CustomerCode IS NOT NULL
GO

DECLARE @query VARCHAR(4000)  
DECLARE @years VARCHAR(2000)  
SELECT  @years = STUFF(( SELECT DISTINCT 
                    '],[' + [Date]  
                    FROM  Revenues2 
                    ORDER BY '],[' + [Date]  
                    FOR XML PATH('')  
                    ), 1, 2, '') + ']' 

SET @query =  
'SELECT * FROM  
(  
  SELECT Subtotal,[Date],CustomerCode  
   FROM Revenues2  
)t  
PIVOT (SUM(Subtotal) FOR [Date] 
IN ('+ @years +')) AS pvt' 

EXECUTE (@query) 

–==========================================

Regards

Cliff

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:59:00+00:00Added an answer on June 11, 2026 at 2:59 am

    I may be wrong, but it seems to me you’re not deleting/commenting the “CREATE VIEW” part when you’re converting to an SP. This is wrong for you cannot define a view from within a stored procedure’s definition – hence the “CREATE VIEW must be(…)” complaint.

    Besides, that code of yours is a bit fragile: once you’ve correctly created the SP, next time it runs it will error with a message of “The SP is already used, you should be using ALTER PROC” or something like that (I’m writing from memory know).

    Code like this should solve both problems:

    USE [Sherwood]
    GO
    
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    -- It's preferable to set these options before creating an SP,
    -- rather than within its execution
    SET NOCOUNT ON
    GO
    IF OBJECT_ID('usp_GetRevenues') IS NOT NULL
        DROP PROC usp_GetRevenues
    GO
    CREATE PROC [dbo].[usp_GetRevenues] 
        @StartDate AS DateTime = '20120301',
        @EndDate AS DateTime = '20120401' AS
    -- BEGIN --This is really optional
    SELECT  
    i.DateOrdered
    ,LTRIM(STR(DATEPART(MONTH,i.DateOrdered))) AS [Month]  
    ,LTRIM(STR(YEAR(i.Dateordered))) AS [Year]  
    ,c.CustomerCode
    ,SUM(i.Jobprice) AS Subtotal 
    FROM dbo.Invoices i
    JOIN dbo.Customers c
    ON i.CustomerID = c.ID
    WHERE i.DateOrdered >= @StartDate 
    AND i.DateOrdered <= @EndDate **<-- and here.**  
    GROUP BY ROLLUP (YEAR(i.DateOrdered), 
    MONTH(i.DateOrdered), i.DateOrdered, c.CustomerCode);
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I know there's a lot of other questions out there that deal with this
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.