作为一个实验,我有兴趣编写一个自定义等于(..)
函数,该功能模仿是[不是]与某些数据库中存在的
功能不同(但是不是Oracle)。
null-ware比较: is \ [not \]独特来自
...提供了一个比较操作员,将两个无效值视为
相同。
Custom 等于(...)
函数可能会这样工作:
- 将两个表达式传递给可以相互比较的函数。
- 该函数将使用一些逻辑(TBD),如果两个值相同,则将返回
true
,如果两个值不同,则 false
。
- 两个零值将被视为相同。
- 可以将nulls与非效果进行比较。例如,
'a'vs. null
将返回 equals = false
,未知。
作为新手,这似乎是一个有趣的主意。
话虽如此,我不知道我如何创建一个能够接受我可能会投入的所有不同数据类型的函数。
通常,我会写下这样的函数:
with function equals(v_text varchar2) return varchar2 --'SAME' or 'DIFFERENT'
但这仅适用于单个数据类型,而不是多个数据类型。
问题:
有没有办法创建一个接受多个数据类型的函数? (考虑到等于(...)
函数>函数想法)
我想Oracle没有创建这样的内置函数可能是一个完全很好的理由。我只是想通过实验学习&问问题。
相关:
As an experiment, I'm interested in writing a custom equals(..)
function that would mimic the is [not] distinct from
functionality that exists in some databases (but not Oracle).
NULL-Aware Comparison: is \[not\] distinct from
...provides a comparison operator that treats two null values as
the same.
The custom equals(...)
function would possibly work like this:
- Pass two expressions to the function that would be compared against each other.
- The function would use some logic (TBD) that would return
true
if the two values are the same, and false
if the two values are different.
- Two nulls would be treated as being the same.
- Nulls could be compared to non-nulls. For example,
'A' vs. null
would return equals = false
, not unknown.
As a novice, that seems like an interesting idea.
With that said, I don't know how I'd create a function that would be capable accepting all the different datatypes that I might throw at it.
Normally, I'd write a function like this:
with function equals(v_text varchar2) return varchar2 --'SAME' or 'DIFFERENT'
But that only works for a single datatype, not multiple datatypes.
Question:
Is there a way to create a function that accepts multiple datatypes? (with the equals(...)
function idea in mind)
I imagine there might be a perfectly good reason why Oracle hasn't created a built-in function like that. I'm just trying to learn-by-experimenting & asking questions.
Related:
发布评论
评论(1)
您可以使用自定义聚合函数来汇总所有从共享超级类型继承的用户定义类型。
适应我在这里的答案:
如果您有类型:
并且您想将它们汇总到唯一列表中:
然后您可以声明用户定义的聚合类型:
然后,您可以将其包装在用户定义的聚合函数中:
然后,如果您使用以下方式调用它,
则输出为:
db<> fiddle noreflowl noreferrer“
You can use a custom aggregation function to aggregate user-defined types that all inherit from a shared super-type.
Adapting my answer here:
If you have the types:
and you want to aggregate them into a unique list into the type:
Then you can declare the user-defined aggregation type:
Then you can wrap it in a user-defined aggregation function:
Then, if you call it using:
Then the output is:
db<>fiddle here