动态SQL不允许我将空变量传递给SSRS

时间:2016-06-27 14:58:00

标签: sql-server reporting-services dynamic-sql

我正在尝试编写一个动态SQL查询,允许最终用户选择不输入某些搜索条件。在此示例中,我希望最终用户能够输入@GLAccountStart的值,而无需输入@GLAccountEnd的值,并且IF ...“语句中@GLAccountStart IS NOT NULL and @GLAccountEnd IS NULL正确执行。

但是,当我尝试在SSRS中运行它时,只输入第一个值会返回0行(它应返回2),并且输入两个变量的值实际上会返回正确的行数。另外,如果我完全从我的脚本中删除@GLAccountEnd变量并使用包含@GLAccountStart的选项运行它,则SSRS会使用正确的输入返回正确的行数。

有没有诀窍或其他方法来实现这个?

USE [DataWarehouse]
GO

/****** Object:  StoredProcedure [dbo].[BrowseCODA]    Script Date: 6/27/2016 8:37:37 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[BrowseCODA] 
@GLAccountStart int,
@GLAccountEnd int
AS
SET NOCOUNT ON;

DECLARE @SQLQuery AS nvarchar(4000)
DECLARE @ParameterDef AS nvarchar(2000)

SET @SQLQuery = 'SELECT *
            FROM [DataWarehouse].[dbo].[VDocDetails]
            WHERE Company+''-''+FullDocNumber IN 
                  (SELECT Company+''-''+FullDocNumber
                   FROM [DataWarehouse].[dbo].[VDocDetails]
                   WHERE (1=1)'
IF (@GLAccountStart IS NOT NULL) AND (@GLAccountEnd IS NOT NULL)
    SET @SQLQuery = @SQLQuery + ' AND (GLAccount >= @GLAccountStart) AND (GLAccount <= @GLAccountEnd)'
IF (@GLAccountStart IS NOT NULL) AND (@GLAccountEnd IS NULL)
    SET @SQLQuery = @SQLQuery + ' AND (GLAccount = @GLAccountStart)'
SET @SQLQuery = @SQLQuery + ')'

SET @ParameterDef = 
'@GLAccountStart int,
 @GLAccountEnd int'

EXECUTE sp_Executesql @SQLQuery,
                      @ParameterDef,
                      @GLAccountStart,
                      @GLAccountEnd

IF @@ERROR <> 0 GOTO ErrorHandler
SET NOCOUNT OFF
RETURN(0)

ErrorHandler:
Return(@@ERROR)

GO

2 个答案:

答案 0 :(得分:1)

我猜@GLAccountEnd没有从SSRS获得空值。 但是,你为什么不尝试简单的SQL?

   SELECT *
     FROM [DataWarehouse].[dbo].[VDocDetails]
    where GLAccount between @GLAccountStart
                       AND  case @GLAccountEnd 
                            when '' then @GLAccountStart
                            else @GLAccountEnd end

或动态:

USE [DataWarehouse]
GO

/****** Object:  StoredProcedure [dbo].[BrowseCODA]    Script Date: 6/27/2016 8:37:37 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[BrowseCODA] 
@GLAccountStart varchar,
@GLAccountEnd varchar
AS
SET NOCOUNT ON;

DECLARE @SQLQuery AS nvarchar(4000)
DECLARE @ParameterDef AS nvarchar(2000)

SET @SQLQuery = 'SELECT *
                   FROM [DataWarehouse].[dbo].[VDocDetails]
                   WHERE (1=1)'
IF (@GLAccountStart <> '') AND (@GLAccountEnd <> '')
    SET @SQLQuery = @SQLQuery + ' AND (GLAccount >= @GLAccountStart) AND (GLAccount <= @GLAccountEnd)'
IF (@GLAccountStart <> '') AND (@GLAccountEnd = '')
    SET @SQLQuery = @SQLQuery + ' AND (GLAccount = @GLAccountStart)'

SET @ParameterDef = 
'@GLAccountStart int,
 @GLAccountEnd int'

EXECUTE sp_Executesql @SQLQuery,
                      @ParameterDef,
                      @GLAccountStart,
                      @GLAccountEnd

IF @@ERROR <> 0 GOTO ErrorHandler
SET NOCOUNT OFF
RETURN(0)

ErrorHandler:
Return(@@ERROR)

GO

答案 1 :(得分:0)

SSRS显然缺乏对参数的输入来表示&#39;&#39;而不是NULL。当我更改动态查询以检查值是否等于&#39;&#39;时,代码按预期工作。