8

我的问题是: 在 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 包装器。

4

2 回答 2

9

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

文档

由于 SQL 没有与 BOOLEAN 等效的数据类型,因此您不能:

  • 将 BOOLEAN 值分配给数据库表列

  • 选择或获取数据库表列的值到 BOOLEAN 变量中

  • 在 SQL 语句、SQL 函数或从 SQL 语句调用的 PL/SQL 函数中使用 BOOLEAN 值

如果您想查找有关内置函数的元数据,那么这篇文章可能会有所帮助。

于 2012-06-14T15:39:34.683 回答
-1

SQL Doesn't work like that. the where statement always looks for a function/column where something is. even if the function works you still have to tell the where statement which value you want True or False

I haven't used Oracle SQL, but looking at what you have there I think that if you write it like this

  select *
  from table t
  where is_prod = True

it will work, if you change the Variable type in your function to something like a Varchar(5) or something similar.

you are actually asking that function to look at several records, so when you have it like you do it acts like a Select Statement and not like a where statement. it will give the value of the function but not filter the where. it will look like a column with true or false values.

When you use the function in a Where statement like this:

SELECT *
FROM table t
WHERE is_Prod

it's like saying:

SELECT *
FROM table t
WHERE Column1

you have to clarify for the WHERE Statement

SELECT *
FROM table t
WHERE Column1 = 'blue' or is_Prod = 'false'

in C# you can use a String as a boolean, if it is null it returns false

in SQL Server it comes out like this

Column2 IS NULL

you still need an operator

* Separator *

as I don't use Oracle I was unable to test this.

http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm

REGEXP_LIKE is a like Statement. so it uses a comparison operator.

you could probably write a regexp in a like statement, although I am sure that it is time consuming and monotonous so they made a function that does it for you.

in other words you still have to use the '= whatever' on the function that you created.

于 2012-06-14T15:37:50.753 回答