表上的动态 JOIN 条件

发布于 2024-09-24 12:55:20 字数 932 浏览 2 评论 0原文

我想避免在 SQL 2008 中使用字符串连接来创建动态 SQL 查询。

如何高效地实现以下功能(如果有的话)?

表存储用户通过使用一个或多个值来搜索表来选择搜索数据库的各种条件。例如,他可以提供 SSN 或 DOB,或者同时提供 SSN 和 DOB。

因此,如果他给出多个值作为输入,那么查询需要查找匹配所有条件的记录。这需要我从下面显示的两个表创建查询,

我总是需要返回 personid,它可能来自 person 表或视图。

用户给出的搜索条件将存储在下面的“条件”表中

conditions
-------------
id,custid,dob,ssn,base

person
--------
id,firstname,ssn,dob

view
--------
id,personid,base

So, IF DOB is to be searched

SELECT p.personid from conditon c 
INNER JOIN person p
ON p.dob=c.dob
WHERE c.condition=1

IF SSN and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
WHERE c.condition=1

IF SSN,DOB and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
AND
p.dob=c.dob
WHERE c.condition=1

I want to avoid string concatenation to create dynamic SQL query in SQL 2008.

How can I achieve the below functionality, if at all, efficiently ?

A table stores the various conditions a user had chosen to search DB by using one or more values to search tables from. For e.g. he can give SSN or DOB or both SSN and DOB.

So if he gives multiple values as input, then the query needs to find records matching ALL the criteria. This needs me to create queries from the below shown two tables

I always need to return personid, which may be from the person table, or the view.

Search conditions given by user will be stored in the 'conditions' table below

conditions
-------------
id,custid,dob,ssn,base

person
--------
id,firstname,ssn,dob

view
--------
id,personid,base

So, IF DOB is to be searched

SELECT p.personid from conditon c 
INNER JOIN person p
ON p.dob=c.dob
WHERE c.condition=1

IF SSN and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
WHERE c.condition=1

IF SSN,DOB and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
AND
p.dob=c.dob
WHERE c.condition=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-10-01 12:55:20

将下面的模式与 IsNull() 或 Coalesce() 函数一起使用

  Where ColumnName = IsNull(@InParameter, ColumnName) 

,或者在连接条件中使用相同的模式

   Join Table a 
      On [Other stuff]
           And ColumnName = IsNull(@InParameter, ColumnName)

然后,如果提供了参数,则 ql 将根据参数值进行过滤或连接,如果未提供或为空,查询将忽略它(ColumnValue = ColumnVale 始终为 true(可空列除外)

编辑:向连接条件添加另一个谓词。因为,在您没有指定参数值的情况下,连接条件将始终为 true,并且,除非连接中存在其他谓词条件,否则您将有效地获得笛卡尔积......

Use the pattern below with IsNull() or Coalesce() function

  Where ColumnName = IsNull(@InParameter, ColumnName) 

or tyhe same pattern in a Join Condition

   Join Table a 
      On [Other stuff]
           And ColumnName = IsNull(@InParameter, ColumnName)

And then, if the parameter is supplied, the ql will filter or join based on the parameter value, if it not supplied or is null, the query will ignore it (ColumnValue = ColumnVale is always true (except for nullable columns)

EDITED: to add another predicate to Join Condition. because, in the case where you did not specifiy a parameter value, the join condition would always be true, and, unless some other predicate condition is in the join, you would effectively get a cartesian product...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文