[英]Apply filter on sql query conditionally
Lets say I have a parameter @Name
in a Stored Procedure. 可以说我在存储过程中有一个@Name
参数。 I want to filter by this parameter only if it is not empty / null. 我想这个参数来筛选, 只有当它不为空/空。 In any other case I want to ignore the filter. 在其他情况下,我想忽略过滤器。
I came up with the following two solutions. 我提出了以下两种解决方案。 For the sake of example let us consider only the case that parameter is empty. 为了举例,让我们仅考虑参数为空的情况。
select *
from MyTable
where (len(rtrim(ltrim(@Name))) > 0 and Name = @Name) or (len(rtrim(ltrim(@Name))) = 0)
and the second one 第二个
@query = 'select * from MyTable'
if (len(rtrim(ltrim(@Name))) > 0)
@query = @query + ' Name = @Name '
Both of the approaches are working as expected. 两种方法都按预期工作。
Note: This question may also suit in Code Review, please comment if you think so, in order to migrate there 注意:此问题也可能适用于Code Review,如果您认为这样,请发表评论,以便在那里进行迁移
It can be simplified like this 可以这样简化
select *
from MyTable
where Name = @Name or @Name = '' or @Name is null
or as mentioned in comments, use NULLIF
to check for empty string then replace it with NULL
then validate it against IS NULL
或如注释中所述,使用NULLIF
检查空字符串,然后将其替换为NULL
然后针对IS NULL
进行验证
where (Name = @Name or nullif(@Name, '') is null)
You don't need to check for length, by default, sql server is trailing-spaces-sensitive (The only exception to this rule is when the right side of the LIKE predicate's expression contains trailing spaces, then the pad is not removed). 您不需要检查长度,默认情况下,SQL Server对尾随空格敏感(此规则的唯一例外是,当LIKE谓词表达式的右侧包含尾随空格时,则不删除填充)。
Take the code below. 使用下面的代码。
DECLARE @Name=' '
IF(@Name='') SELECT 1 ELSE SELECT 0
If you run the above code above you will get a result of 1. In your case, you can drop the LTRIM and RTRIM and simply test for equality against an empty string literal. 如果在上面运行上述代码,您将得到1的结果。在您的情况下,可以删除LTRIM和RTRIM并仅针对空字符串文字测试是否相等。
select *
from MyTable
where ((@Name='' OR @Name IS NULL)OR(Name = @Name))
OR 要么
IF(@Name='') SET @Name=NULL
select *
from MyTable
where (@Name IS NULL OR Name = @Name)
if you are working with dynamic sql in stored procedure try something like this . 如果您正在存储过程中使用动态sql,请尝试执行以下操作。 It is better to use different variables for main select query and dynamic where query which can be extended easily . 最好对主选择查询和动态查询使用不同的变量,这些变量可以轻松扩展。 using this approach it will be easy to maintain when you proc becomes lengthy. 使用这种方法,当处理过程变得冗长时,将易于维护。
declare @finalquery varchar(max)
declare @mainSelectquery nvarchar(500);
declare @whereCondtions varchar (1000);
declare @DateParam datetime
set @mainSelectquery=''
set @whereCondtions =''
set @finalquery =''
set @DateParam=getdate()
set @mainSelectquery = 'select * from tblOrders where 1=1 '
set @whereCondtions = ' and Order_site =''TSN'''
set @whereCondtions = @whereCondtions + ' AND CAST(ORDER_APPROVED_DATE_LT AS DATE)=CAST(GETDATE() AS DATE)'
set @finalquery =( @mainSelectquery + @whereCondtions)
print @finalquery
---- You can further extend this by adding more where condition based on the parameter pass in stored proc
if (@OrderID !=0)
begin
set @whereCondtions = ' OrderID='+str ( @stateRefID )
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.