How can I create a quot;dynamicquot; WHERE clause?(我怎样才能创建一个“动态的?WHERE 子句?)
问题描述
首先:谢谢!
我完成了我的另一个项目和一个大惊喜:现在一切正常:-)感谢 SO 的一些有帮助的思想家!
I finished my other project and the big surprise: now everything works as it should :-) Thanks to some helpful thinkers of SO!
所以我开始下一个项目.
我想得到这样的东西:
正如您所注意到的,这可能是一个很长的 where 子句.表名是一个不会改变的静态属性.field1
, field2
, ... (!) 并且内容可以改变.
As you noticed this can be a very long where-clause.
tablename is a static property which does not change.
field1
, field2
, ... (!) and the contents can change.
所以我需要一个选项来在 PL/SQL 中的递归函数中构建 SQL 语句.我真的不知道要搜索什么,所以我在这里要求链接甚至要搜索的词..
请不要开始争论递归函数是否真的需要或者它的缺点是什么 - 这是不是的问题;-)
Please dont start to argue about wether the recursive function is really needed or what its disadvanteges - this is not in question ;-)
如果你能帮我创建一个类似 SQL-String 的东西,这样以后就可以成功执行 SELECT,那就太好了!
If you could help me to create something like an SQL-String which will later be able to do a successful SELECT this would be very nice!
我可以通过递归函数每次生成更长的字符串,但我无法从中生成 SQL 语句..
Iam able to go through the recursive function and make a longer string each time, but I cannot make an SQL statement from it..
哦,还有一件事:我通过 xmlType(xmldom.domdocument 等)获取字段和内容我可以从 xmltype 获取字段和内容,例如在 clob 中
Oh, one additional thing: I get the fields and contents by a xmlType (xmldom.domdocument etc) I can get the field and the content for example in a clob from the xmltype
推荐答案
目标是从 WHERE 子句中的可变数量的过滤器中动态组装一个语句.我不确定递归在哪里适合所有这些,所以我将只使用一个数组来处理参数:
The object is to dynamically assemble a statement from a variable number of filters in the WHERE clause. I'm not sure where recursion fits into all this, so I will just use an array to handle the parameters:
这个表被传递给一个函数,该函数在数组周围循环.对于数组中的每个条目,它以 <name> 格式将一行附加到 WHERE 子句中.= '<值>'.您可能需要更复杂的过滤 - 不同的运算符、显式数据类型转换、绑定变量 - 但这是总体思路.
This table is passed to a function, which loops around the array. For each entry in the array it appends a line to the WHERE clause in the format <name> = '<value>'. Probably you will require more sophisticated filtering - different operators, explicit data type conversion, bind variables - but this is the general idea.
最后,为了执行这个查询,我们需要填充一个数组类型的局部变量并将结果返回给一个引用游标.
Finally to execute this query we need to populate a local variable of the array type and return the result to a ref cursor.
编辑
在他们问题的最后一段中,OP 说他们正在使用 XML 来通过标准.这个要求不会显着改变我的原始实现的形状.循环只需要驱动一个 XPath 查询而不是一个数组:
In the last paragraph of their question the OP says they are using XML to pass the criteria. This requirement doesn't dramatically change the shape of my original implementation. The loop simply needs to drive off an XPath query instead of an array:
可以看出,这个版本返回的结果和之前一样...
As can be seen, this version returns the same results as before...
这篇关于我怎样才能创建一个“动态"的?WHERE 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!