参数中的多个值

时间:2016-10-05 23:19:05

标签: ssrs-2008-r2

我的查询有3个参数开始日期,结束日期和位置。查询存储在SQL存储过程中,我在SSRS中调用存储过程(EXEC dbo.ups_Calls @Start,@ End,@ Location)(参见下面的代码)。

sort=Total_ix - count_ix

问题:从SSR查询框调用存储过程(见下文)只是从SSRS位置参数中拉出一个站点,即使我选择了多个位置,但是如果我将查询本身嵌入SSRS然后我能够拉多个站点。

enter image description here

1 个答案:

答案 0 :(得分:0)

使用存储过程时,它需要能够处理多个参数。 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