如何绕过“ora-14551 无法在查询中执行DML操作”


如创建以下程序:

create or replace function FUN_GET_ETL_MSG return varchar2 is
  Result varchar2(200);
  cursor cur_row is
    select id, msg
      from (select t.*
                  ,sum(lengthb(t.msg)) over(order by crt_dttm) len
              from p17_etl_sms t
             where 1 = 1
               and sms_dttm is null)
     where 1 = 1
       and len <= 30;
begin
  Result := '';

  for c_r in cur_row loop
    Result := Result || '[' || c_r.msg || ']';
    update p17_etl_sms
       set sms_dttm = sysdate
     where 1 = 1
       and id = c_r.id;
  end loop;
  commit;

  return(Result);
end FUN_GET_ETL_MSG;

那么在执行 select FUN_GET_ETL_MSG from dual 时,就会报 “ ora-14551 无法在查询中执行DML操作”

现可以通过 自治事务,解决此问题

create or replace function FUN_GET_ETL_MSG return varchar2 is
  Result varchar2(200);
  cursor cur_row is
    select id, msg
      from (select t.*
                  ,sum(lengthb(t.msg)) over(order by crt_dttm) len
              from p17_etl_sms t
             where 1 = 1
               and sms_dttm is null)
     where 1 = 1
       and len <= 30;
  pragma autonomous_transaction;
begin
  Result := '';

  for c_r in cur_row loop
    Result := Result || '[' || c_r.msg || ']';
    update p17_etl_sms
       set sms_dttm = sysdate
     where 1 = 1
       and id = c_r.id;
  end loop;
  commit;

  return(Result);
end FUN_GET_ETL_MSG;

OK, 可以执行 select FUN_GET_ETL_MSG from dual  了。




你可能感兴趣的:(c,function,null,sms,fun)