使用变量过滤多列

filter on multiple columns with variables

我有一个 SQL table 看起来像这样:

DECLARE @BI_ResponsibleID INT , @CategoryID  INT , @ChangeRequestorID INT 

SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]

我希望能够根据我的变量值过滤

E.G 有时只有@BI_ResponsibleID 其他时候@CategoryID 和@ChangeRequestorID 等等.....

这可能与我的变量有关吗?

尝试这样的事情:

声明@BI_ResponsibleID INT,@CategoryID INT,@ChangeRequestorID INT

SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SELECT TOP 4
     [BI_ResponsibleID],
     [CategoryID],
     [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE (ISNULL([BI_ResponsibleID],0) = @BI_ResponsibleID) 
    AND (ISNULL([CategoryID],0) = @CategoryID)
    AND (ISNULL([ChangeRequestorID],0) = @ChangeRequestorID)

我的解决方案:

像这样使用空参数的默认值

SET @BI_ResponsibleID = ISNULL(@BI_ResponsibleID, 0 );
SET @CategoryID = ISNULL(@CategoryID, 0 );
SET @ChangeRequestorID = ISNULL(@ChangeRequestorID, 0 );

然后试试这个:

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE (@BI_ResponsibleID = 0 OR [BI_ResponsibleID] = @BI_ResponsibleID)
  AND (@CategoryID = 0 OR [CategoryID] = @CategoryID)
  AND (@ChangeRequestorID = 0 OR [ChangeRequestorID] = @ChangeRequestorID)

您可以使用下面的动态查询。我假设如果变量的值为零,您不想在该列上进行过滤。

// Not tested code
DECLARE @BI_ResponsibleID INT , @CategoryID  INT , @ChangeRequestorID INT 
DECLARE @sqlCommand nvarchar(1000)


SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SET @sqlCommand = 'TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity] where 1 = 1 '

if(@BI_ResponsibleID != 0)
 set @sqlCommand +=  'AND BI_ResponsibleID = @BI_ResponsibleID '

 if(@CategoryID != 0)
 set @sqlCommand +=  'AND CategoryID = @CategoryID ' 

 if(@ChangeRequestorID != 0)
 set @sqlCommand +=  'AND ChangeRequestorID = @ChangeRequestorID '

EXEC sp_executesql @sqlCommand, N'@BI_ResponsibleID INT, @CategoryID INT, @ChangeRequestorID INT', @BI_ResponsibleID, @CategoryID, @ChangeRequestorID;

因为我没有表和数据,所以无法检查这个查询。但这就是实现它的方法。

DECLARE @BI_ResponsibleID INT , @CategoryID  INT , @ChangeRequestorID INT 

SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE ( 1 = 1 OR ( @BI_ResponsibleID IS NOT NULL AND  Some Other condition using @BI_ResponsibleID))
    AND ( 1 = 1 OR ( @CategoryID IS NOT NULL AND  Some Other condition using @CategoryID))
    AND ( 1 = 1 OR ( @ChangeRequestorID IS NOT NULL AND  Some Other condition using @ChangeRequestorID))
DECLARE @BI_ResponsibleID INT , @CategoryID  INT , @ChangeRequestorID INT 

SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE ( BI_ResponsibleID = @BI_ResponsibleID OR @BI_ResponsibleID IS NULL )
      AND ( CategoryID = @CategoryID OR @CategoryID IS NULL )
      AND ( ChangeRequestorID = @ChangeRequestorID OR @ChangeRequestorID IS NULL )

您需要做的是,包含 OR 条件来检查 NULL 参数,如下所示:

DECLARE @BI_ResponsibleID INT , @CategoryID  INT , @ChangeRequestorID INT 

SET @BI_ResponsibleID = 5
SET @CategoryID = 3
SET @ChangeRequestorID = 4

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE 
    (BI_ResponsibleID = @BI_ResponsibleID OR @BI_ResponsibleID IS NULL)
    AND (CategoryID = @CategoryID OR @CategoryID IS NULL)
    AND (ChangeRequestorID = @ChangeRequestorID OR @ChangeRequestorID IS NULL)

您可能想像这样通过存储过程执行此操作:

CREATE PROCEDURE MyProcName
    @BI_ResponsibleID = NULL
    @CategoryID = NULL
    @ChangeRequestorID = NULL

AS

SELECT TOP 4
      [BI_ResponsibleID],
      [CategoryID],
      [ChangeRequestorID]   
FROM [BI_Planning].[dbo].[tlbActivity]
WHERE 
    (BI_ResponsibleID = @BI_ResponsibleID OR @BI_ResponsibleID IS NULL)
    AND (CategoryID = @CategoryID OR @CategoryID IS NULL)
    AND (ChangeRequestorID = @ChangeRequestorID OR @ChangeRequestorID IS NULL)

END