暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle PL/SQL函数中的DML操作

ASKTOM 2019-05-02
228

问题描述

先生,

1) 我还可以在PL/SQL函数中执行DML (插入,更新,删除) 吗?
2) 我可以显式调用触发器吗?

专家解答

1.是的。您可能希望执行此操作以将自动生成的主键或其他默认值返回给客户端。

但这确实意味着您无法从SQL调用该函数。

create table t (
  c1 int generated as identity
);

create or replace function f
  return int as
  retval int;
begin

  insert into t 
  values ( default )
  returning c1 into retval;
  
  return retval;
end f;
/

select f from dual;

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

exec dbms_output.put_line ( 'val = ' || f );

val = 1

select * from t;

C1   
    1 


2.没有

create or replace trigger trg
before insert on t
for each row
begin
  dbms_output.put_line ( 'fired' ); 
end;
/

exec trg;

BEGIN trg; END;

      *
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'TRG' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

exec chris.trg;

BEGIN chris.trg; END;

      *
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'CHRIS.TRG' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

insert into t values ( default );

fired

1 row created.

文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论