在数据库中自定义了一个Function,
假设function名字是getCount通过查询语句来调用
select getCount as iCount fromDual;这个getCount是否可以同时做写数据库的操作呢?

解决方案 »

  1.   

    --可以的,只要你自定义的getCount function里面有写数据库的语句,比如:
    --insert into table(col_1,col_2) values(value_1,value_2)之类的cml语句--如果你的自定义函数里面没有写数据库的dml语句,那么这个自定义函数就不会做写数据库的操作
    */
      

  2.   

    --做个实验不就知道了
    Wrote file afiedt.buf  1  create or replace function test_fun
      2  return varchar2
      3  as
      4  begin
      5  insert into dept(deptno) values(55);
      6  return 'T';
      7* end;
    scott@YPCOST> /Function created.scott@YPCOST> select test_fun from dual;
    select test_fun from dual
           *
    ERROR at line 1:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "SCOTT.TEST_FUN", line 5--看看错误提示
      

  3.   

    Select查询来调用Function,
    无法执行插入操作么?
      

  4.   


    --也可以,要使用自治事务
    --不过有dml语句的还是使用存储过程吧  不要使用函数了
    Wrote file afiedt.buf  1  create or replace function test_fun
      2  return varchar2
      3  as
      4  PRAGMA AUTONOMOUS_TRANSACTION;--自治事务
      5  begin
      6  insert into dept(deptno) values(55);
      7  commit;--提交
      8  return 'T';
      9* end;
    scott@YPCOST> /Function created.scott@YPCOST> select test_fun from dual;TEST_FUN
    --------------------------------------------------
    Tscott@YPCOST> select * from dept;    DEPTNO DNAME          LOC
    ---------- -------------- -------------
            55
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
      

  5.   

    -------------------
    谢谢啦,
    现在可以Select查询Function的同时,来写数据库啦
      

  6.   

    一般在select 中调用函数不能使用、dml。
    但在命令行中调用时可以的
    exec C:=fun
      

  7.   

    可以。但业务中不推荐,在查询中可以不理会事务,如果有了delete,update,insert,那就需要处理事务了,另外,一旦业务数据出现问题,很难查找。
      

  8.   

    嗯,一般来说在函数中不推荐使用DML
      

  9.   

    数据库的Function和存储过程可以相互调用么?