我将这段代码拼凑在一起,为经销商的每张发票小计创建一个支点。它有效,并产生正确的结果。但是,当我尝试将其转换为存储过程时,臭名昭着的红色线条出现在SQL Server管理工作室的“CREATE VIEW Revenues”下,错误说明:
'CREATE VIEW必须是批处理中唯一的语句',我不知道还有足够的T-SQL继续进行,因为我需要能够从ASP.NET / C#Web应用程序执行它。
我正在尝试通过以下方式创建SP:
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
前面是,最后有一个END语句。
2)参数对于能够过滤数月的数据是必要的,但我似乎无法传递参数或使用代码按原样使用变量。
提前致谢。
代码如下 - 显示我需要过滤变量的位置。该代码仅适用于where子句中“1/1/2012”格式的日期,但需要能够如图所示传递变量。
- ================================
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)
- ==========================================
此致
崖
答案 0 :(得分:1)
您可以使用临时表来尝试此操作。
像
这样的东西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
INTO #Revenues
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)
SELECT
LTRIM(STR([YEAR])) + '-' + STUFF([Month],1,0, REPLICATE('0', 2 - LEN([Month])))
AS [Date]
,Subtotal
,CustomerCode
INTO #Revenues2
FROM #Revenues
WHERE CustomerCode IS NOT NULL
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)
DROP TABLE #Revenues
DROP TABLE #Revenues2
答案 1 :(得分:1)
我可能错了,但在您转换为SP时,我似乎并未删除/评论“创建视图”部分。这是错误的,因为您无法在存储过程的定义中定义视图 - 因此“创建视图必须是(...)”投诉。
此外,你的代码有点脆弱:一旦你正确创建了SP,下次运行它就会出错,并显示“已经使用了SP,你应该使用ALTER PROC”之类的东西那样的(我是从记忆中写的)。
这样的代码应该可以解决这两个问题:
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