Sql server sql简单存储过程-筛选器

Sql server sql简单存储过程-筛选器,sql-server,stored-procedures,Sql Server,Stored Procedures,我有一个名为tblProducts的表,它有3列:intID、dateMain、intStatus 我有这样一个存储过程: ALTER PROCEDURE [dbo].[spList_Report] @id INT, @startDate DATETIME = NULL, @endDate DATETIME = NULL, @includeStatus1 BIT, @includeStatus2 BIT, @includeStatus3 BIT, @includeSt

我有一个名为tblProducts的表,它有3列:intID、dateMain、intStatus

我有这样一个存储过程:

ALTER PROCEDURE [dbo].[spList_Report]
  @id INT,
  @startDate DATETIME = NULL,
  @endDate DATETIME = NULL,
  @includeStatus1 BIT,
  @includeStatus2 BIT,
  @includeStatus3 BIT,
  @includeStatus4 BIT

AS
  SET NOCOUNT ON

  SELECT *
  FROM
    tblProducts as products
  WHERE 
    product.intID = @id
    AND product.dateMain >= @startDate 
    AND product.dateMain <= @endDate
ALTER过程[dbo]。[spList\u报告]
@id INT,
@startDate DATETIME=NULL,
@endDate DATETIME=NULL,
@包括状态1位,
@包括状态2位,
@包括状态3位,
@includeStatus 4位
作为
不计较
挑选*
从…起
TBL产品作为产品
哪里
product.intID=@id
和product.dateMain>=@startDate
和product.dateMain试试这个:

AND product.status1 = COALESCE(NULLIF(@includeStatus1, 0), product.status1)

如果@IncludeStats1为null或0,则实际上将忽略它。如果它是1,那么它将过滤结果。

假设0不是有效状态,将此代码添加到WHERE子句应该有效。如果0是有效状态,则为参数传递NULL而不是0,或者在选择之前将0转换为NULL,这仍然有效

and intStatus in (@includeStatus1*1, @includeStatus2*2, @includeStatus3*3, @includeStatus4*4)

如果参数作为0传入,它应该只包含与0匹配的行,还是在这种情况下要包含所有行?如果位为true,则我希望检索状态为1的行,否则不检索。将更新问题+1。但是如果
status1
允许空值,则需要更多的代码。