SQL - 过滤器值为空时全选

2021-09-10 00:00:00 sql tsql sql-server

我的 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 + '%') 

相关文章