【发布时间】:2015-01-23 14:54:14
【问题描述】:
我有一个 C# 表单,用于在具有不同选项的表中进行搜索,用户可以过滤搜索过程。
我有 4 个文本框,例如 txt1 、 txt2 、txt3 和 txt4。
用户可以填写每个文本框进行搜索,例如 txt1 或 txt1 和 txt2 或 txt1 和 txt 和 txt4 或所有这些文本框,而将另一个留空并使用这些文本框的所有组合。那么如何编写一个选择过程来涵盖所有这些选项。
我写了这个程序,但它没有帮助:
CREATE proc sp_searchZ
@minprice bigint=null,@maxprice bigint=null,@minarea int=null,@maxarea int=null,@location nvarchar(50)=null,@kind nvarchar(50)=null
as
SELECT *
FROM Landtbl
WHERE ((@minprice is null and @maxpriceis null) or ([Price] between @minprice and @maxprice))
and
((@minarea is null and @maxarea is null) or ([area] between @minarea and @maxarea))
and ((@location is null)or([location]=@location))
and ((@kind is null) or ([kind]=@kind))
【问题讨论】:
-
“它不会有帮助”是什么意思?我认为您的基本逻辑是合理的,您是否期望获得零记录?
-
@maxpriceis null是您的存储过程中的错字还是仅在这个问题中? -
对不起,我知道了....但是忘记了我的代码...当用户动态过滤和填充一些文本框时我该怎么办?
-
在继续之前,您需要阅读这篇关于捕获所有查询的文章。 sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries
-
另外两个 cmets。您不应该在实际代码中使用 select *。第二个是您的过程名称的前缀。我个人不能忍受前缀,因为它们不会增加清晰度,并且更难在列表中找到它们。我在这里说什么的原因是因为你的前缀是“sp_”。这是由 MS 为系统过程保留的。这意味着如果您不定义架构,引擎将首先在 sys 架构中查找,如果 sql server 有更新,它可能会导致您的 proc 无法使用。
标签: c# sql-server stored-procedures