我正在为SSRS报告创建一个存储过程,通常当我有一个多值参数时,我会使用split函数来分离和计算结果。
但我对这个存储过程的问题是我需要添加三个额外的值来添加到数据集中;我在这里做过:
CREATE PROCEDURE dbo.rpt_DataSet_AgentClassType
AS
BEGIN
SET NOCOUNT ON;
SELECT
x.ID, x.ClassType
FROM
(SELECT 997 AS [ID], 'Desktops' AS [ClassType]
UNION ALL
SELECT 998 AS [ID], 'Mobile Devices' AS [ClassType]
UNION ALL
SELECT 999 AS [ID], 'Mobile Apps' AS [ClassType]
UNION ALL
SELECT ACT.[id] AS [ID], ACT.[description] AS [ClassType]
FROM dbo.AgentClassTypes AS [ACT]
WHERE ACT.id NOT IN (14, 15, 16)
AND ACT.active = 1
) AS x
ORDER BY
x.ID;
END;
结果数据集如下所示:
ID ClassType
--------------------------------
1 TWRA Central License Sales
3 General Agent
4 Service Desk
5 Internet
....
18 State Parks
19 TWRA Special
997 Desktops
998 Mobile Devices
999 Mobile Apps
我在这里要完成的是,如果用户选择997 - 桌面,998 - 移动设备或999 - 移动应用程序;存储过程不应使用split函数,因为表中不存在该值,而是在表中搜索数据集中的特定字段。
这是我的存储过程:
ALTER PROCEDURE dbo.rpt_HarvestByAgentType
@StartDate DATE,
@EndDate DATE,
@AgentClassType VARCHAR(MAX),
@SpeciesID VARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#HarvestByAgentTypeResults') IS NOT NULL
DROP TABLE #HarvestByAgentTypeResults;
CREATE TABLE #HarvestByAgentTypeResults
(
TotalHarvest INT,
HarvestDate DATE,
AgentClassType VARCHAR(100),
SpeciesID INT,
SpeciesType VARCHAR(50)
);
-- Desktops
IF @AgentClassType IN ('997')
BEGIN
INSERT INTO #HarvestByAgentTypeResults (TotalHarvest, HarvestDate, AgentClassType, SpeciesID, SpeciesType)
SELECT
COUNT(x.HarvestRecordID) AS [TotalHarvest],
x.HarvestDate AS [HarvestDate],
'Desktop' AS [AgentClassType],
x.SpeciesID AS [SpeciesID],
x.SpeciesType AS [SpeciesType]
FROM
(SELECT
CHR.HarvestRecordID,
CAST(CHR.HarvestDate AS DATE) AS [HarvestDate],
CHR.SpeciesID AS [SpeciesID],
HST.SpeciesType AS SpeciesType
FROM
dbo.CustomerHarvestReports AS [CHR]
INNER JOIN
dbo.Agents AS [A] ON A.agentID = CHR.agentID
INNER JOIN
dbo.AgentClassTypes AS [ACT] ON ACT.id = A.agentClassTypeID
INNER JOIN
dbo.HarvestSpeciesTypes AS [HST] ON HST.SpeciesID = CHR.SpeciesID AND HST.InActive = 0
WHERE
CHR.HarvestDate >= @StartDate
AND CHR.HarvestDate <= @EndDate
AND HST.SpeciesID In (SELECT Item
FROM dbo.fnSplit(@SpeciesID,','))
AND CHR.isInvalidated = 0
AND ((CHR.httpUserAgentString LIKE '%Win%' AND CHR.httpUserAgentString NOT LIKE '%Windows Phone%' ) -- Windows Machines excluding Windows phones
OR (CHR.httpUserAgentString LIKE '%Linux%' AND CHR.httpUserAgentString NOT LIKE '%Android') -- Linux machines excluding Android phones
OR CHR.httpUserAgentString LIKE '%Macintosh%' -- Mac books
OR CHR.httpUserAgentString LIKE '%CrOS%') -- Chrome Books
) x
GROUP BY
x.HarvestDate, x.SpeciesID, x.SpeciesType
END;
-- Mobile Devices
ELSE IF @AgentClassType IN ('998')
BEGIN
INSERT INTO #HarvestByAgentTypeResults(TotalHarvest, HarvestDate, AgentClassType, SpeciesID, SpeciesType)
SELECT
COUNT(x.HarvestRecordID) AS [TotalHarvest],
x.HarvestDate AS [HarvestDate],
'MobileDevice' AS [AgentClassType] ,
x.SpeciesID AS [SpeciesID] ,
x.SpeciesType AS [SpeciesType]
FROM ( SELECT CHR.HarvestRecordID ,
CAST(CHR.HarvestDate AS DATE) AS [HarvestDate] ,
ACT.[description] agentClass ,
CHR.SpeciesID AS [SpeciesID] ,
HST.SpeciesType AS SpeciesType
FROM dbo.CustomerHarvestReports AS [CHR]
LEFT OUTER JOIN dbo.Agents AS [A] ON A.agentID = CHR.agentID
LEFT OUTER JOIN dbo.AgentClassTypes AS [ACT] ON ACT.id = A.agentClassTypeID
LEFT OUTER JOIN dbo.HarvestSpeciesTypes AS [HST] ON HST.SpeciesID = CHR.SpeciesID AND HST.InActive = 0
WHERE CHR.HarvestDate >= @StartDate
AND CHR.HarvestDate <= @EndDate
AND HST.SpeciesID In ( SELECT Item FROM dbo.fnSplit(@SpeciesID,','))
AND CHR.isInvalidated = 0
AND (( httpUserAgentString LIKE '%iPhone%' AND httpUserAgentString LIKE '%Version%' ) -- iPhones
OR ( httpUserAgentString LIKE '%Android%' AND httpUserAgentString NOT LIKE '%Version%' ) -- Androids
OR ( httpUserAgentString LIKE '%iPad%' AND httpUserAgentString LIKE '%Version%' ) -- iPads
OR ( httpUserAgentString LIKE '%BB10%' ) -- Blackberries
OR ( httpUserAgentString LIKE '%Windows Phone%' )) -- Windows Phones
) x
GROUP BY x.HarvestDate ,
x.SpeciesID ,
x.SpeciesType
END;
-- Mobile Apps
ELSE IF @AgentClassType IN ('999')
BEGIN
INSERT INTO #HarvestByAgentTypeResults
( TotalHarvest ,
HarvestDate ,
AgentClassType ,
SpeciesID ,
SpeciesType
)
SELECT COUNT(x.HarvestRecordID) AS [TotalHarvest] ,
x.HarvestDate AS [HarvestDate],
'MobileApp' AS [AgentClassType] ,
x.SpeciesID AS [SpeciesID] ,
x.SpeciesType AS [SpeciesType]
FROM ( SELECT CHR.HarvestRecordID ,
CAST(CHR.HarvestDate AS DATE) AS [HarvestDate] ,
ACT.[description] agentClass ,
CHR.SpeciesID AS [SpeciesID] ,
HST.SpeciesType AS SpeciesType
FROM dbo.CustomerHarvestReports AS [CHR]
LEFT OUTER JOIN dbo.Agents AS [A] ON A.agentID = CHR.agentID
LEFT OUTER JOIN dbo.AgentClassTypes AS [ACT] ON ACT.id = A.agentClassTypeID
LEFT OUTER JOIN dbo.HarvestSpeciesTypes AS [HST] ON HST.SpeciesID = CHR.SpeciesID AND HST.InActive = 0
WHERE CHR.HarvestDate >= @StartDate
AND CHR.HarvestDate <= @EndDate
AND HST.SpeciesID In ( SELECT Item FROM dbo.fnSplit(@SpeciesID,','))
AND CHR.isInvalidated = 0
AND (( httpUserAgentString LIKE '%iPhone%' AND httpUserAgentString NOT LIKE '%Version%' AND httpUserAgentString NOT LIKE '%Windows Phone%' ) -- iPhones
OR ( httpUserAgentString LIKE '%Android%' AND httpUserAgentString LIKE '%Version%' )) -- Androids
) x
GROUP BY x.HarvestDate ,
x.SpeciesID ,
x.SpeciesType
END;
ELSE IF (@AgentClassType NOT IN ('997','998','999'))
BEGIN
INSERT INTO #HarvestByAgentTypeResults
( TotalHarvest ,
HarvestDate ,
AgentClassType ,
SpeciesID ,
SpeciesType
)
SELECT COUNT(x.HarvestRecordID) AS [TotalHarvest] ,
x.HarvestDate AS [HarvestDate] ,
x.AgentTypeClass AS [AgentTypeClass] ,
x.SpeciesID AS [SpeciesID] ,
x.SpeciesType AS [SpeciesType]
FROM ( SELECT CHR.HarvestRecordID ,
CAST(CHR.HarvestDate AS DATE) AS [HarvestDate] ,
ACT.[description] AS [AgentTypeClass] ,
CHR.SpeciesID AS [SpeciesID] ,
HST.SpeciesType AS [SpeciesType]
FROM dbo.CustomerHarvestReports AS [CHR]
INNER JOIN dbo.Agents AS [A] ON A.agentID = CHR.agentID
INNER JOIN dbo.AgentClassTypes AS [ACT] ON ACT.id = A.agentClassTypeID
INNER JOIN dbo.HarvestSpeciesTypes AS [HST] ON HST.SpeciesID = CHR.SpeciesID AND HST.InActive = 0
WHERE CHR.HarvestDate >= @StartDate
AND CHR.HarvestDate <= @EndDate
AND HST.SpeciesID IN ( SELECT Item FROM dbo.fnSplit(@SpeciesID, ','))
AND A.agentClassTypeID IN ( SELECT Item FROM dbo.fnSplit(@AgentClassType ,','))
AND CHR.isInvalidated = 0
) AS x
GROUP BY x.HarvestDate ,
x.AgentTypeClass ,
x.SpeciesID ,
x.SpeciesType
ORDER BY x.HarvestDate ,
x.AgentTypeClass,
x.SpeciesType;
END
SELECT TotalHarvest ,
HarvestDate ,
AgentClassType ,
SpeciesID ,
SpeciesType FROM #HarvestByAgentTypeResults
ORDER BY HarvestDate, SpeciesType, AgentClassType;
DROP TABLE #HarvestByAgentTypeResults;
END;
当我从Management Studio执行存储过程时,如果我只在@AgentClasstype
变量中输入997或998或999,我会得到结果。但是,如果我包含诸如1,3,5等等的值... - 则不会返回我期望的997,998或999的数据集。
希望我已经清楚地解释了这一点 - 很难正确地编写脚本 - 似乎很难写出来寻求帮助。