0

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

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

因此,如果用户为项目名称输入“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 语句并使变量使用比应有的更复杂)

4

2 回答 2

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

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

于 2013-04-18T00:46:36.753 回答
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
于 2013-04-18T00:53:12.987 回答