當你使用存儲過程,它需要能夠處理多個參數。 SSRS通常通過多值參數,逗號分隔值,所以你可以使用下面的函數與存儲過程結合,以處理該值:
CREATE FUNCTION [dbo].[udf_Split]
(
@RowData NVARCHAR(MAX),
@Delimeter NVARCHAR(MAX)
)
RETURNS @RtnValue TABLE
(
ID INT IDENTITY(1,1),
Data NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @Iterator INT
SET @Iterator = 1
DECLARE @FoundIndex INT
SET @FoundIndex = CHARINDEX(@Delimeter,@RowData)
WHILE (@FoundIndex>0)
BEGIN
INSERT INTO @RtnValue (data)
SELECT
Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))
SET @RowData = SUBSTRING(@RowData,
@FoundIndex + DATALENGTH(@Delimeter)/2,
LEN(@RowData))
SET @Iterator = @Iterator + 1
SET @FoundIndex = CHARINDEX(@Delimeter, @RowData)
END
INSERT INTO @RtnValue (Data)
SELECT Data = LTRIM(RTRIM(@RowData))
RETURN
END
GO
存儲過程:
CREATE PROC dbo.Calls
@Start DATETIME=NULL,
@End DATETIME=NULL,
@Location VARCHAR(25)=NULL
AS
BEGIN
SELECT CALLDATE, LOCATION
FROM CALL_DETAILS WITH (NOLOCK)
WHERE CALLDATE BETWEEN @START AND @END
AND LOCATION IN (SELECT DATA FROM [dbo].[udf_Split](@Location,','))
END
GO