我可以在oracle中使用用户定义的函数删除行吗?
我创建了一个用户定义的函数来删除一些数据。它不适用于删除,但适用于选择。我是甲骨文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_PURGEDATA('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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题的答案是“不”。
如果您删除错误“处理”,您会发现删除失败,并出现如下异常:
,即您无法在 SELECT 语句中调用的函数内执行插入、更新或删除。
要在 IDE 或 SQL Plus 中执行此函数,请将其包装在更多 PL/SQL 中,如下所示:
但是,您需要首先向函数添加 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:
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:
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.)
如果添加 PRAGMA AUTONOMOUS_TRANSACTION,则可以在 SELECT 中使用的函数内执行 DML。例如:
但是如果可能的话,您肯定希望避免这种方法。一般来说,如果在 SELECT 中使用函数,则无法知道该函数将被执行多少次。
You can perform DML inside a function used in a SELECT if you add PRAGMA AUTONOMOUS_TRANSACTION. For example:
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.
是的,您可以使用 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
为什么不创建一个带有 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).