如果参数值=“ALL”,如何忽略条件

时间:2011-07-13 07:24:42

标签: sql-server-2008

我找到了这个链接

How to return all records if parameter is null

我需要,

当参数值为ALL时,忽略参数/条件,如上所述。

如何实现这一目标。

下面的信息,TASK:如果参数= ALL

,则应忽略null
Declare @psqluid varchar(15) = 'admin'
Declare @psqlstartdate datetime = '06/01/2010'
Declare @psqlenddate datetime = '06/28/2012'
Declare @psqlcompany varchar(45)
Declare @psqlapplication varchar(10) = 'APP2'
Declare @psqlprogram varchar(10)= 'PROG1'
SELECT * FROM [TMSFINAL].[dbo].[TMTMS] 
WHERE (@psqluid IS NULL  OR [TMSFINAL].[dbo].[TMTMS].[tmsUserID]=@psqluid) 
AND [TMSFINAL].[dbo].[TMTMS].[tmsDate] >= @psqlstartdate 
AND [TMSFINAL].[dbo].[TMTMS].[tmsDate] <= @psqlenddate 
AND (@psqlcompany IS NULL OR [TMSFINAL].[dbo].[TMTMS].[tmsCompany] = @psqlcompany) 
AND [TMSFINAL].[dbo].[TMTMS].[tmsApplication] = @psqlapplication 
AND [TMSFINAL].[dbo].[TMTMS].[tmsProgram] = @psqlprogram

1 个答案:

答案 0 :(得分:1)

IF @param!="ALL"
BEGIN
  SET @Sql += 'Condition'
END

不行吗?

加入:

WHERE @psqluid = "ALL" OR [TMSFINAL].[dbo].[TMTMS].[tmsUserID]=@psqluid ...