我可以在oracle中使用用户定义的函数删除行吗?

发布于 2024-11-06 03:44:46 字数 455 浏览 5 评论 0原文

我创建了一个用户定义的函数来删除一些数据。它不适用于删除,但适用于选择。我是甲骨文9i。

该函数是这样的:

create or replace function UFN_PURGEDATA(INPUTID IN VarChar2) return number is
  Result number;
begin

  Result := 0;


   DELETE FROM MyTable WHERE MyTable.ID=INPID;

  COMMIT; 

   Result := 1;
 EXCEPTION WHEN OTHERS THEN    

  return(Result);
end UFN_PURGEDATA;

然后我使用 select UFN_PUR​​GEDATA('test') from Dual 来运行它,但得到结果 0

I created a user defined function to delete some data. It doesn't work with delete but works with select. I am Oracle 9i.

The function is something like this:

create or replace function UFN_PURGEDATA(INPUTID IN VarChar2) return number is
  Result number;
begin

  Result := 0;


   DELETE FROM MyTable WHERE MyTable.ID=INPID;

  COMMIT; 

   Result := 1;
 EXCEPTION WHEN OTHERS THEN    

  return(Result);
end UFN_PURGEDATA;

Then I use select UFN_PURGEDATA('test') from dual to run it but got result 0.

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

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

发布评论

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

评论(4

抱着落日 2024-11-13 03:44:46

你的问题的答案是“不”。

如果您删除错误“处理”,您会发现删除失败,并出现如下异常:

ORA-14551: 无法执行 DML
查询内的操作

,即您无法在 SELECT 语句中调用的函数内执行插入、更新或删除。

要在 IDE 或 SQL Plus 中执行此函数,请将其包装在更多 PL/SQL 中,如下所示:

declare
  l_result number;
begin
  l_result := my_function(123);
end;

但是,您需要首先向函数添加 RETURN 语句,否则它将失败。

(注意,我在上面用引号说“处理”,因为它实际上是“处理不当”——它以一种非常无益的方式完全掩盖了实际问题。)

The answer to your question is "no".

If you remove your error "handling" you will find that the delete is failing with an exception like:

ORA-14551: cannot perform a DML
operation inside a query

i.e. you cannot perform an insert, update or delete from within a function called in a SELECT statement.

To execute this function in an IDE or SQL Plus, wrap it in some more PL/SQL like this:

declare
  l_result number;
begin
  l_result := my_function(123);
end;

However, you will need to add a RETURN statement to your function first otherwise it will fail.

(NB I said "handling" above in quotes because it is really "mishandling" - it completely disguises the actual problem in a very unhelpful way.)

送你一个梦 2024-11-13 03:44:46

如果添加 PRAGMA AUTONOMOUS_TRANSACTION,则可以在 SELECT 中使用的函数内执行 DML。例如:

create or replace function UFN_PURGEDATA(INPUTID IN VarChar2) return number is
    pragma autonomous_transaction;
begin
  DELETE FROM MyTable WHERE MyTable.ID=INPUTID;
  COMMIT; 
  return 1;
  EXCEPTION WHEN OTHERS THEN    
  return 0;
end UFN_PURGEDATA;
/

但是如果可能的话,您肯定希望避免这种方法。一般来说,如果在 SELECT 中使用函数,则无法知道该函数将被执行多少次。

You can perform DML inside a function used in a SELECT if you add PRAGMA AUTONOMOUS_TRANSACTION. For example:

create or replace function UFN_PURGEDATA(INPUTID IN VarChar2) return number is
    pragma autonomous_transaction;
begin
  DELETE FROM MyTable WHERE MyTable.ID=INPUTID;
  COMMIT; 
  return 1;
  EXCEPTION WHEN OTHERS THEN    
  return 0;
end UFN_PURGEDATA;
/

But you definitely want to avoid this approach if possible. In general, there's no way to know how many times a function will be executed if it's used in a SELECT.

谢绝鈎搭 2024-11-13 03:44:46

是的,您可以使用 Oracle 中的用户定义函数删除行,但不能从 SELECT 语句中删除行。

您的代码存在一些问题:
- 如果您的函数不引发异常,则不会返回值
- 不得在 SELECT 语句中使用执行 DML 的函数;如果删除异常块,则会收到 ORA-14551

Yes you can delete rows using user-defined functions in Oracle, but not from within a SELECT statement.

There are a couple of problems with your code:
- you don't return a value if your function does not raise an exception
- you must not use a function performing DML in a SELECT statement; if you remove your exception block, you get an ORA-14551

丶情人眼里出诗心の 2024-11-13 03:44:46

为什么不创建一个带有 OUT 参数返回数字的过程(而不是函数)?如果不使用自治事务技巧,Oracle 不希望您运行具有“副作用”的函数(在选择中使用)(可以理解为什么我们不希望选择导致 DML 更改)。

Why not create a procedure (instead of function) with OUT parameter returning the number? Without doing the autonomous transaction trick, Oracle doesn't want you running functions (used in selects) with "side-effects" (understandable why we don't want a select to result in DML changes).

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