如何正确编写此数据库搜索查询?

时间:2013-04-18 00:35:32

标签: asp.net sql tsql search dotnetnuke

我正在尝试编写一个基于许多不同表单字段搜索数据库的查询。我希望查询按输入的字段进行过滤,并且只是忽略未在表单上输入的任何字段。例如:

表单字段: -项目名 -项目编号 - 项目经理

因此,如果用户为项目名称输入“Joe”,它应该在名称字段中以任何形式的“joe”返回所有结果,而不管该记录中的其他值是什么。

我一直试图用严格的SQL来完成这个,而且我的结果不正确(但预期)。我有一些非常沉重的疑问,如果他们相当遥远,我会道歉。

使用AND语句为所有条件设置了第一个查询。我意识到它不起作用,因为数据库中的NULL值仍然不会被通配符拾取。

    <ListDataSource CommandText="
SELECT [surveyid], 
       [projectnumber], 
       [siteno], 
       [person], 
       [subatpdate], 
       [projectname], 
       [priorsurveyor], 
       [siteaddress], 
       [sitecity], 
       [sitestate], 
       [sitezip], 
       [survey], 
       [zoning], 
       [environmental], 
       [emg_projectnumber], 
       [zoning_projectnumber], 
       [surveyor_projectnumber] 
FROM   tblrawprojectdatabase 
WHERE  [projectnumber] LIKE '%' + @projectnumber + '%' 
       AND [siteno] LIKE '%' + @siteno + '%' 
       AND [person] LIKE '%' + @person + '%' 
       AND [subatpdate] LIKE '%' + @subatpdate + '%' 
       AND [projectname] LIKE '%' + @projectname + '%' 
       AND [priorsurveyor] LIKE '%' + @priorsurveyor + '%' 
       AND [siteaddress] LIKE '%' + @siteaddress + '%' 
       AND [sitecity] LIKE '%' + @sitecity + '%' 
       AND [sitestate] LIKE '%' + @sitestate + '%' 
       AND [sitezip] LIKE '%' + @sitezip + '%' 
       AND [survey] LIKE '%' + @survey + '%' 
       AND [zoning] LIKE '%' + @zoning + '%' 
       AND [environmental] LIKE '%' + @environmental + '%' 
       AND [emg_projectnumber] LIKE '%' + @emg_projectnumber + '%' 
       AND [zoning_projectnumber] LIKE '%' + @zoning_projectnumber + '%' 
       AND [surveyor_projectnumber] LIKE '%' + @surveyor_projectnumber + '%' "

我的第二次尝试检查空值。这样做效果会好一些,除非您输入“Jim”作为项目经理,它还会返回所有带有null的记录作为项目经理值。

       <ListDataSource CommandText="
SELECT [surveyid], 
       [projectnumber], 
       [siteno], 
       [person], 
       [subatpdate], 
       [projectname], 
       [priorsurveyor], 
       [siteaddress], 
       [sitecity], 
       [sitestate], 
       [sitezip], 
       [survey], 
       [zoning], 
       [environmental], 
       [emg_projectnumber], 
       [zoning_projectnumber], 
       [surveyor_projectnumber] 
FROM   tblrawprojectdatabase 
WHERE  ( [projectnumber] LIKE '%' + @projectnumber + '%' 
          OR [projectnumber] IS NULL ) 
       AND ( [siteno] LIKE '%' + @siteno + '%' 
              OR [siteno] IS NULL ) 
       AND ( [person] LIKE '%' + @person + '%' 
              OR [person] IS NULL ) 
       AND ( [priorsurveyor] LIKE '%' + @priorsurveyor + '%' 
              OR [priorsurveyor] IS NULL ) 
       AND ( [siteaddress] LIKE '%' + @siteaddress + '%' 
              OR [siteaddress] IS NULL ) 
       AND ( [sitecity] LIKE '%' + @sitecity + '%' 
              OR [sitecity] IS NULL ) 
       AND ( [sitestate] LIKE '%' + @sitestate + '%' 
              OR [sitestate] IS NULL ) 
       AND ( [sitezip] LIKE '%' + @sitezip + '%' 
              OR [sitezip] IS NULL ) 
       AND ( [survey] LIKE '%' + @survey + '%' 
              OR [survey] IS NULL ) 
       AND ( [zoning] LIKE '%' + @zoning + '%' 
              OR [zoning] IS NULL ) 
       AND ( [environmental] LIKE '%' + @environmental + '%' 
              OR [environmental] IS NULL ) 
       AND ( [emg_projectnumber] LIKE '%' + @emg_projectnumber + '%' 
              OR [emg_projectnumber] IS NULL ) 
       AND ( [zoning_projectnumber] LIKE '%' + @zoning_projectnumber + '%' 
              OR [zoning_projectnumber] IS NULL ) 
       AND ( [surveyor_projectnumber] LIKE '%' + @surveyor_projectnumber + '%' 
              OR [surveyor_projectnumber] IS NULL ) "

是否有使用OR并使其作为短路评估工作,以便在满足第一个条件时无法检查空值?

谢谢,关于如何完成我的搜索查询的任何建议都会很棒。

更新

我在初始帖子中遗漏的一些有价值的信息是我正在使用名为XModPro的DotNetNuke模块。我不知道有什么办法成功使用条件和循环逻辑,我需要用ASP构建查询,因为模块限制你使用他们的标签(据我所知,只提供if和select语句并使变量使用比应有的更复杂)

2 个答案:

答案 0 :(得分:1)

在构建CommandText变量时,在asp代码中执行一些条件逻辑。例如,如果在ProjectName文本框中未输入任何内容,请不要在查询中包含它。此外,您可以考虑使用OR逻辑而不是AND。这取决于你想要达到的目标。

这是一个如何做到这一点的大致想法。这不是真正的代码,它只是显示了方法:

string WhereClause = "where 1 = 2 "
if a project name was specified.
WhereClause = WhereClause + something about the project name.
etc

另外,请记住包含查询参数。

答案 1 :(得分:0)

然后,在临时表中添加所有内容,删除你想要的任何东西并从临时表中返回。

SELECT [surveyid], [projectnumber], [siteno], [person], [subatpdate], [projectname], 
[priorsurveyor], [siteaddress], [sitecity], [sitestate], [sitezip], [survey], [zoning], 
[environmental], [emg_projectnumber], [zoning_projectnumber], [surveyor_projectnumber] 
INTO #temp
FROM tblRawProjectDatabase
 WHERE ([projectnumber] LIKE '%' + @projectnumber + '%' OR [projectnumber] IS NULL) AND 
([siteno] LIKE '%' + @siteno + '%' OR [siteno] IS NULL) AND 
([person] LIKE '%' + @person + '%' OR [person] IS NULL) AND 
([priorsurveyor] LIKE '%' + @priorsurveyor + '%' OR [priorsurveyor] IS NULL) AND 
([siteaddress] LIKE '%' + @siteaddress + '%' OR [siteaddress] IS NULL) AND 
([sitecity] LIKE '%' + @sitecity + '%' OR [sitecity] IS NULL) AND 
([sitestate] LIKE '%' + @sitestate + '%' OR [sitestate] IS NULL) AND 
([sitezip] LIKE '%' + @sitezip + '%' OR [sitezip] IS NULL) AND 
([survey] LIKE '%' + @survey + '%' OR [survey] IS NULL) AND 
([zoning] LIKE '%' + @zoning + '%' OR [zoning] IS NULL) AND 
([environmental] LIKE '%' + @environmental + '%' OR [environmental] IS NULL) AND 
([emg_projectnumber] LIKE '%' + @emg_projectnumber + '%' OR [emg_projectnumber] IS NULL) AND 
([zoning_projectnumber] LIKE '%' + @zoning_projectnumber + '%' OR [zoning_projectnumber] IS NULL) AND 
([surveyor_projectnumber] LIKE '%' + @surveyor_projectnumber + '%' OR [surveyor_projectnumber] IS NULL)

DELETE FROM #temp WHERE ProjectManaer IS NULL
SELECT * FROM #temp
DROP TABLE #temp