If-Else Sproc&在SSRS中调用多参数SProc

时间:2013-11-08 13:46:31

标签: tsql stored-procedures reporting-services

SSRS中的存储过程相当新。

我已经处理了大量报告,其参数直接在SSRS数据集中传递,这对我来说有点新鲜,似乎让我感到有些困惑。

我要求合并具有完全相同布局的2个报告,然后根据SSRS的下拉选项执行报告。

我的存储过程如下所示。


USE [ReportsDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[rpt_WithoutExternalReferences]
(
    @XNumber INT,
    @YNumber INT
)   
AS

BEGIN
SET NOCOUNT ON

IF (@XNumber = NULL)
    BEGIN
        SELECT 
            adviser_account.adviser_account_id,
            dealer.old_system_reference,
            external_reference.external_reference AS Missing_refernce,
            entity.name AS Name,
            entity.given_names As Given_Names,
            entity_address.property_name,
        FROM Composer_adviser_account AS adviser_account
        INNER JOIN Composer_entity AS entity
        ON adviser_account.entity_id = entity.entity_id
        LEFT JOIN Composer_party_external_reference AS external_reference
        ON adviser_account.adviser_account_id = external_reference.party_id  
        WHERE
            external_reference.party_type_id = 3
    END
ELSE
    BEGIN
        IF (@YNumber = NULL)
            BEGIN
                SELECT 
                    dealer.dealer_id,
                    dealer.old_system_reference,
                    dealer.fsa_firm_reference_id as Missing_refernce,
                    entity.name AS Name,
                    entity.given_names AS Given_Names,
                    entity_address.property_name,
                FROM Composer_dealer AS dealer
                INNER JOIN Composer_entity AS entity
                ON dealer.entity_id = entity.entity_id
                INNER JOIN Composer_address AS entity_address
                ON entity.entity_id = entity_address.entity_id
                WHERE dealer.fsa_firm_reference_id IS NULL
            END
        END
    END
END

现在,我想从SSRS调用此存储过程,用户可以选择XNumber Null或YNumber NULL,然后运行相应的代码部分。

非常感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请注意您的NULL比较:您必须使用IS关键字,并且应将输入参数设为可选:

CREATE PROCEDURE [dbo].[rpt_WithoutExternalReferences]
(
    @XNumber INT = NULL,
    @YNumber INT = NULL
)   
AS BEGIN

    SET NOCOUNT ON

    IF ((@XNumber IS NULL AND YNumber IS NULL) OR
        (@XNumber IS NOT NULL AND YNumber IS NOT NULL))
        RAISERROR('Invalid arguments', 16, 1);

    IF (@XNumber IS NULL) BEGIN

        SELECT      adviser_account.adviser_account_id
                    ,dealer.old_system_reference 
                    ,external_reference.external_reference AS Missing_refernce
                    ,entity.name AS Name
                    ,entity.given_names As Given_Names
                    ,entity_address.property_name 
        FROM        Composer_adviser_account AS adviser_account
        INNER JOIN  Composer_entity AS entity
                ON  adviser_account.entity_id = entity.entity_id
        LEFT JOIN   Composer_party_external_reference AS external_reference
                ON  adviser_account.adviser_account_id = external_reference.party_id  
        WHERE       external_reference.party_type_id = 3

    END ELSE IF (@YNumber IS NULL) BEGIN

        SELECT      dealer.dealer_id 
                    ,dealer.old_system_reference 
                    ,dealer.fsa_firm_reference_id as Missing_refernce
                    ,entity.name AS Name
                    ,entity.given_names AS Given_Names
                    ,entity_address.property_name 
        FROM        Composer_dealer AS dealer
        INNER JOIN  Composer_entity AS entity
                ON  dealer.entity_id = entity.entity_id
        INNER JOIN  Composer_address AS entity_address
                ON  entity.entity_id = entity_address.entity_id
        WHERE       dealer.fsa_firm_reference_id IS NULL

    END

END