如何为SSRS报告设置项目列表输出的存储过程

时间:2015-02-12 22:52:51

标签: sql-server-2008 tsql stored-procedures ssrs-2008

我有一个查询,实质上是通过一个表列表(其列表从表中的字段中提取并存储在游标中)来比较无效数据,并且应该只返回列表有无效数据的表格。

我主要挣扎的是,我无法弄清楚如何正确设置存储过程以SSRS可以使用的方式输出列表。我在SSRS中需要的只是表格列表。

当我运行SP时,所有的查询都是'结果显示在SSMS结果网格中 - 正如预期的那样,并且在“消息”选项卡中,我确实获得了包含无效数据的表的正确列表。

在我的研究中,为了弄清楚如何做到这一点,我只能找到返回单个值的示例。正如我所说,我需要返回一个值列表。

这是我的存储过程(注释代码是尝试将表列表存储到临时表中以便SSRS从中获取列表):

USE MasterFiles
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('sp_GetInvalidXrefCategories') IS NOT NULL
    DROP PROCEDURE sp_GetInvalidXrefCategories
GO

CREATE PROCEDURE dbo.sp_GetInvalidXrefCategories

@includeAR bit,
@includeClin bit,
@includePayroll bit,
@includeGL bit,
@includeAP bit,
@includeDeletes bit

AS


-- Get the list of xref categories
DECLARE @listOfCategories TABLE
    ( CategoryName varchar(100) )
INSERT INTO @listOfCategories (CategoryName)
SELECT Category FROM MasterFiles..Categories
WHERE Category IN (SELECT Category FROM MasterFiles..xref) AND TableToValidate = 'xref'
ORDER BY Category

-- Store the list of categories so we can loop through them
DECLARE categoryList CURSOR FOR
SELECT CategoryName FROM @listOfCategories

DECLARE @validationCategory nvarchar(100)

-- To store the list of invalid categories for later output
DECLARE @listOfInvalidCategories TABLE
    ( CategoryName varchar(100) )

--CREATE TABLE #tmpInvalidCatsList ( catName varchar(100) )
--DECLARE @Output varchar(max)

OPEN categoryList
FETCH NEXT FROM categoryList INTO @validationCategory

WHILE @@FETCH_STATUS = 0
    BEGIN
        IF ((SELECT COUNT(*) FROM MasterFiles..xref WHERE category = @validationCategory) = 0 OR (@validationCategory = 'Employee_No' OR @validationCategory = 'County'))
            FETCH NEXT FROM categoryList INTO @validationCategory
        ELSE
            BEGIN
                DECLARE @validationTable nvarchar(100)
                DECLARE @validationField nvarchar(100)
                DECLARE @validationQuery nvarchar(max)

                SET @validationTable = (SELECT ValidationTable FROM MasterFiles..Categories WHERE Category = @validationCategory AND TableToValidate = 'xref')
                SET @validationField = (SELECT ValidationField FROM MasterFiles..Categories WHERE Category = @validationCategory AND TableToValidate = 'xref')

                --PRINT 'Validating category ' + @validationCategory + ' against Angus table ' + @validationTable

                IF @validationCategory = 'action_code'
                     -- Action_Code needs Action_Type and Action_Code concatenated to compare Angus table to xref
                    BEGIN
                        IF @includeDeletes = 0
                            BEGIN
                                SET @validationQuery = N'SELECT DISTINCT category FROM MasterFiles..xref WHERE Category = ''validationCategory'' AND (new_value NOT IN (SELECT Action_Type + '' '' + ValidationField FROM Angus..validationTable) AND new_value != ''DELETE'' OR new_value IS NULL OR new_value = '''')'
                            END
                        ELSE
                            BEGIN
                                SET @validationQuery = N'SELECT DISTINCT category FROM MasterFiles..xref WHERE Category = ''validationCategory'' AND (new_value NOT IN (SELECT Action_Type + '' '' + ValidationField FROM Angus..validationTable) OR new_value IS NULL OR new_value = '''')'
                            END
                    END
                ELSE
                    BEGIN
                        IF @includeDeletes = 0
                            BEGIN
                                SET @validationQuery = N'SELECT DISTINCT category FROM MasterFiles..xref WHERE Category = ''validationCategory'' AND (new_value NOT IN (SELECT ValidationField FROM Angus..validationTable) AND new_value != ''DELETE'' OR new_value IS NULL OR new_value = '''')'
                            END
                        ELSE
                            BEGIN
                                SET @validationQuery = N'SELECT DISTINCT category FROM MasterFiles..xref WHERE Category = ''validationCategory'' AND (new_value NOT IN (SELECT ValidationField FROM Angus..validationTable) OR new_value IS NULL OR new_value = '''')'
                            END
                    END

                SET @validationQuery = REPLACE(@validationQuery,'validationCategory',@validationCategory)
                SET @validationQuery = REPLACE(@validationQuery,'validationField',@validationField)
                SET @validationQuery = REPLACE(@validationQuery,'validationTable',@validationTable)

                --PRINT @validationQuery
                DECLARE @rowCount int

                EXECUTE sp_executesql @validationQuery

                SET @rowCount = @@ROWCOUNT

                IF @rowCount > 0
                    BEGIN
                        INSERT INTO @listOfInvalidCategories (CategoryName)
                        SELECT @validationCategory
                        --INSERT INTO #tmpInvalidCatsList
                        --SELECT @validationCategory
                    END


                FETCH NEXT FROM categoryList INTO @validationCategory
            END
    END

-- OUTPUT THE LIST OF INVALID CATEGORIES
DECLARE @invalidCategory nvarchar(100)
DECLARE invalidCategoryList CURSOR FOR
SELECT CategoryName FROM @listOfInvalidCategories

OPEN invalidCategoryList
FETCH NEXT FROM invalidCategoryList INTO @invalidCategory

WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT @invalidCategory
        FETCH NEXT FROM invalidCategoryList INTO @invalidCategory
    END

--SELECT @Output as [Output]
--FROM #tmpInvalidCatsList

--RETURN @Output

CLOSE categoryList
DEALLOCATE categoryList

CLOSE invalidCategoryList
DEALLOCATE invalidCategoryList

0 个答案:

没有答案