SQL - 过滤器值为空时全选 [英] SQL - Select all when filter value is empty

查看:43
本文介绍了SQL - 过滤器值为空时全选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ASP.net Web 应用程序中有一个 SQL 查询,如下所示:

I have a SQL query in my ASP.net web app that looks like this:

SELECT * FROM [Records] WHERE ([title] LIKE '%' + @title + '%')

@title,当然是页面上一个文本框的值.

@title, of course, is the value of a text box on the page.

我的问题是,为什么当文本框为空时,这不返回任何内容?我怎样才能让它返回一切,就像逻辑告诉我它应该那样?

My question is, why, when the text box is empty, does this return nothing? And how can I make it return everything, like logic tells me it ought to?

推荐答案

SELECT * FROM [Records] 
WHERE @title IS NULL OR LEN(@Title) = 0 OR ([title] LIKE '%' + @title + '%') 

这篇关于SQL - 过滤器值为空时全选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆