gpt4 book ai didi

sql - 如何编写仅在 where 子句中使用的函数,该函数在没有比较运算符的情况下计算为 true?

转载 作者:行者123 更新时间:2023-12-04 11:41:52 25 4
gpt4 key购买 nike

我的问题是:
在 Oracle 中 regexp_like 在 where 子句中单独工作,而不必与 1 或 0 或字符串进行比较。该函数只能在评估 case 语句或 where 子句中的某些内容时调用。由于它无法描述(尝试搜索数据字典),我想知道如何编写一个以相同方式工作的函数。

例如:

function is_prod
returns boolean
is
l_var boolean := false;
begin
if sys_context('userenv','db_unique_name') = '"PROD_SERVER"' then
l_var := true;
end if;
return l_var;
end;

该函数可以编译,但不能在如下 SQL 语句中使用:
select *
from table t
where is_prod

因为我收到以下错误:ORA-00920:关系运算符无效。

将它与数字或 true 进行比较也不起作用。

我在哪里可以找到 regexp_like 的代码库,或者我需要做些什么才能像 regexp_like 一样工作?

注意:我环顾了几个小时,发现 Oracle 的 regexp 函数实际上是 java 调用,但这意味着它们仍然需要一个 pl/sql 包装器。

最佳答案

基本上,oracle 有一个 bool 数据类型 只有对于 PLSQL。
因此,只要您留在 plsql 中,您就可以使用它们,但不能在 SQL 中使用。

来自 documentation :

Because SQL has no data type equivalent to BOOLEAN, you cannot:

  • Assign a BOOLEAN value to a database table column

  • Select or fetch the value of a database table column into a BOOLEAN variable

  • Use a BOOLEAN value in a SQL statement, SQL function, or PL/SQL function invoked from a SQL statement



如果您想查找有关内置函数的元数据,则可能是 this post可以帮助。

关于sql - 如何编写仅在 where 子句中使用的函数,该函数在没有比较运算符的情况下计算为 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com