使用内联函数在SSRS中创建数据集

时间:2016-12-20 21:04:06

标签: sql-server stored-procedures reporting-services sql-function

我有一个基于SQL的报告,我正在从Crystal Reports迁移到SSRS。旧方法使用调用存储函数的存储过程。新方法的目的是将所有报告逻辑嵌入SSRS报告中。这背后的原因是存储过程和函数不是目标数据库的一部分。我们不希望有一个额外的数据库只包含一些存储的函数和过程。

现在的基本结构是:

select (a bunch of fields)
from (a bunch of joins)
where (conditions)
    and specific_value in (select value
                           from stored_function(inputs)
                          )

存储的函数执行一些分隔符处理。我需要在SSRS 中仅运行,而不在数据库中存储任何内容。我可以将存储过程转换为SSRS中的数据集,但我无法弄清楚如何处理stored_function。

-----编辑-----这是问题中的代码:

CREATE FUNCTION [dbo].[fn_split] (
    @SourceString sql_variant,
    @Delimiter nvarchar(10) = N',')
    RETURNS @Values TABLE(Position smallint IDENTITY, cValue varchar(2000) , ncValue nvarchar(2000))
AS
BEGIN
    DECLARE @NormalString varchar(2000), @NationalString nvarchar(2000),
        @NormalDelimiter varchar(10), @NationalDelimiter nvarchar(10),
        @IsNationalChar bit, @Position int,
        @NormalValue varchar(2000), @NationalValue nvarchar(2000)
    SET @Delimiter = COALESCE(@Delimiter, N',')
    SET @IsNationalChar = CASE
                WHEN SQL_VARIANT_PROPERTY(@SourceString,'BaseType') IN ('char','varchar')
                THEN 0
                WHEN SQL_VARIANT_PROPERTY(@SourceString,'BaseType') IN ('nchar','nvarchar')
                THEN 1
                  END
    IF @IsNationalChar IS NULL RETURN
    IF @IsNationalChar = 0
    BEGIN
        SET @NormalDelimiter = @Delimiter
        SET @NormalString = CAST(@SourceString AS varchar(2000))
        IF LEFT(@NormalString,LEN(@NormalDelimiter)) = @NormalDelimiter
            SET @NormalString = SUBSTRING(@NormalString,LEN(@NormalDelimiter) + 1, 2000)
        IF RIGHT(@NormalString,LEN(@NormalDelimiter)) <> @NormalDelimiter
            SET @NormalString = @NormalString + @NormalDelimiter
        WHILE(1 = 1)
        BEGIN
            SET @Position = CHARINDEX(@NormalDelimiter,@NormalString) - 1
            IF @Position <= 0 BREAK
            SET @NormalValue = LEFT(@NormalString,@Position)
            SET @NormalString = STUFF(@NormalString,1,@Position + LEN(@NormalDelimiter),'')
            INSERT INTO @Values(cValue) VALUES(@NormalValue)
        END
    END
    ELSE IF @IsNationalChar = 1
    BEGIN
        SET @NationalDelimiter = @Delimiter
        SET @NationalString = CAST(@SourceString AS varchar(2000))
        IF LEFT(@NationalString,LEN(@NationalDelimiter)) = @NationalDelimiter
            SET @NationalString = SUBSTRING(@NationalString,LEN(@NationalDelimiter) + 1,2000)
        IF RIGHT(@NationalString,LEN(@NationalDelimiter)) <> @NationalDelimiter
            SET @NationalString = @NationalString + @NationalDelimiter
        WHILE(1 = 1)
        BEGIN
            SET @Position = CHARINDEX(@NationalDelimiter,@NationalString) - 1
            IF @Position <= 0 BREAK
            SET @NationalValue = LEFT(@NationalString,@Position)
            SET @NationalString = STUFF(@NationalString,1,@Position + LEN(@NationalDelimiter),'')
            INSERT INTO @Values (ncValue) VALUES(@NationalValue)
        END
    END
    RETURN
END

在查询结束时调用该函数(fn_split)。

select (a bunch of fields)
from (a bunch of joins)
where (conditions)
    and specific_value in (select value
                           from stored_function(input_value)
                          )

我试图在不使用数据库对象(函数或过程)的情况下重新创建它。我对SSRS报告中的代码感到满意。

1 个答案:

答案 0 :(得分:0)

只是个主意。您可以使用2个数据集:

select (a bunch of fields)
from (a bunch of joins)
where (conditions)
and specific_value in @Algo

select value 
from stored_function(inputs)