我有一个 SQL Server 存储过程,我想用它在我的应用程序 (c#) 中创建报告。
因为过程中的表格是动态创建的,所以我无法使用它来创建报告。
如何使用此过程在我的应用程序中创建报告?
我正在使用 VS2010、SQL Server 2008 R2
我在 VS 中创建了一个数据集,但不知道如何从那里使用它。报表向导无法使用我创建的数据集。
我考虑过以某种方式使用它来创建一个视图,然后从中生成一个数据集,但无法弄清楚。
我希望我已经为某人提供了足够的信息来给我一些指导。
PROCEDURE sp_RD_Reporting_View_ExportedData_For_Period
(
@StartDate date,
@EndDate date
)
AS
CREATE TABLE #tmp
(
StartDate DATETIME,
EndDate DATETIME,
FomatedDate VARCHAR(20)
)
--Calculate the date ranges
;WITH Nbrs ( n ) AS (
SELECT 0 UNION ALL
SELECT 1+n FROM Nbrs WHERE n < 6 )
INSERT INTO #tmp
SELECT
DATEADD(DAY,-n,@StartDate),
DATEADD(DAY,-n,@EndDate),
convert(varchar, DATEADD(DAY,-n,@EndDate), 110)
FROM
Nbrs
ORDER BY
-n
--The date columns for the pivot
DECLARE @cols VARCHAR(MAX)
SELECT @cols = COALESCE(@cols + ','+QUOTENAME(FomatedDate),
QUOTENAME(FomatedDate))
FROM
#tmp
--Declaring some dynamic sql and executing it
DECLARE @query NVARCHAR(4000)=
N'SELECT
*
FROM
(
SELECT ExportData.EmployeeID AS EmpID,
ABRAempInfo.EmpFullName AS Name,
ExportData.PayType AS PayType,
tmp.FomatedDate,
SUM(ExportData.Quantity) AS Hours
FROM ExportData
JOIN ABRAempInfo
ON ExportData.EmployeeID = ABRAempInfo.EmpID
JOIN #tmp AS tmp
ON ExportData.ChargeDate = tmp.FomatedDate
WHERE ChargeDate BETWEEN tmp.StartDate AND tmp.EndDate
GROUP BY ExportData.EmployeeID, ExportData.PayType, tmp.FomatedDate, ABRAempInfo.EmpFullName
) AS p
PIVOT
(
SUM(Hours)
FOR FomatedDate IN ('+@cols+')
) AS pvt'
EXECUTE(@query)
DROP TABLE #tmp