Cannot perform a dml statement within a query: ora-14551

I'm trying to call my function from a SELECT statement, and I keep getting the error mentioned in the title of the thread.
Could anybody help me out with this?
HEre's my code:
BEGIN
DECLARE CURSOR cur_order IS
  SELECT idshopper, orderplaced
  FROM bb_basket
  WHERE idshopper=p_shopperid
  FOR UPDATE NOWAIT;
BEGIN
FOR rec_order IN cur_order LOOP
  IF rec_order.orderplaced=1 THEN lv_count_num:=lv_count_num+1;
  END IF;
  IF rec_order.orderplaced=0 THEN lv_count_num:=0;
  END IF;
END LOOP;
RETURN lv_count_num;
END;
END;
SELECT idshopper, num_purch_sf(idshopper)
FROM bb_shopper
WHERE idshopper=23;

And you probably want to rewrite this to a single query like this:
select s.idshopper
     , nvl(sum(b.orderplaced),0)
  from bb_shopper s
     , bb_basket b
where s.idshopper = b.idshopper (+)
   and s.idshopper = 23
group by s.idshopperThis way you won't have extra code to maintain and you prevent the contextswitches from SQL to PL/SQL.
Note: query untested
Regards,
Rob.
Message was edited by:
Rob van Wijk
Looking closer at the original code, I see that your query doesn't have an order by and so the outcome of your function is undeterministic. Maybe my query is what you intended, but beware: my query won't necessarily yield the same result

Similar Messages

  • Getting error SQL Error : ORA-14551: cannot perform a DML operation inside a query

    Hi gurus ,
    Your help is greatly appreciated ..
    I am doing some changes in the fucntion for an existing package .Introducing the new below check , am updating one of the tables based on a if condition ..
           IF  numALLOWED_COUNT >= numLAST_COUNT_ADDED+1  THEN
                     blnGDS_Allowed :=True;
                      varSTMT := 'UPDATE PROD.TMS_PROCESS_COUNTER ';
                      varSTMT := varSTMT ||' SET last_count_added = last_count_added+1';
                      varSTMT := varSTMT ||' WHERE process_name = ''DAILY_GDS_COUNT''';
                      varSTMT := varSTMT ||' AND COUNTER_IND = ''750FD130''';
                     PROC_LOG('Update Tms_Process_counter varSTMT --' || varSTMT);
                     IF INSERT_BATCH(99,varSTMT) > 0 THEN
                        NULL;
                     END IF;
    Function for insert_batch :
    UNCTION INSERT_BATCH(numTABLE_ID IN NUMBER, varSQL_STATEMENT IN VARCHAR2) RETURN NUMBER IS
    varINSERT_BATCH_STMT  VARCHAR2(32767)     := NULL;
    varADD_REC_TYPE       BATCH_TABLES.ADD_REC_TYPE%TYPE;
    BEGIN
        PROC_LOG( 'INSIDE INSERT_BATCH IRC : ' || varSQL_STATEMENT );  --IRC 9/20 UC
        INSERT INTO BATCH_STATEMENT(QUEUE_ID,TABLE_ID,STATEMENT,QUEUE_SEQUENCE_ID)
        VALUES (numQUEUE_ID,numTABLE_ID,varSQL_STATEMENT,1);
    RETURN 1;
    EXCEPTION WHEN OTHERS THEN
        PROC_LOG('Failed in INSERT_BATCH');
        PROC_LOG('SQL Error : ' || SUBSTR(SQLERRM,1,1000));
        RETURN -1;
    END INSERT_BATCH;
    desc PROD.BATCH_STATEMENT
      QUEUE_ID           NUMBER(15)                 NOT NULL
      TABLE_ID           NUMBER(2)                  NOT NULL
      STATEMENT          VARCHAR2(4000 BYTE)        NOT NULL
      QUEUE_SEQUENCE_ID  NUMBER(5)                  NOT NULL
    Some how when its calling the insert_batch , its giving me the error in the logs as below:
    04:01:41 - Update Tms_Process_counter varSTMT --UPDATE PROD.TMS_PROCESS_COUNTER  SET last_count_added = last_count_added+1 WHERE process_name = 'DAILY_GDS_COUNT' AND COUNTER_IND = '750FD130'
    04:01:41 - INSIDE INSERT_BATCH IRC : UPDATE PROD.TMS_PROCESS_COUNTER  SET last_count_added = last_count_added+1 WHERE process_name = 'DAILY_GDS_COUNT' AND COUNTER_IND = '750FD130'
    04:01:41 - Failed in INSERT_BATCH
    04:01:41 - SQL Error : ORA-14551: cannot perform a DML operation inside a query

    Some how when its calling the insert_batch , its giving me the error in the logs as below:
    04:01:41 - SQL Error : ORA-14551: cannot perform a DML operation inside a query
    Yes - and the exception is telling you EXACTLY what the problem is. You have a query
    IF INSERT_BATCH(99,varSTMT) > 0 THEN
    And you are performing a DML operation inside that query:
    INSERT INTO BATCH_STATEMENT(QUEUE_ID,TABLE_ID,STATEMENT,QUEUE_SEQUENCE_ID)
        VALUES (numQUEUE_ID,numTABLE_ID,varSQL_STATEMENT,1);
    Like the exception says: you can't do that.
    You need to call the function using PL/SQL and capture the return value into a variable. Then test that variable:
    myVar := INSERT_BATCH(99,varSTMT);
    if myVar > 0 THEN

  • "cannot perform a DML operation inside a query" error when using table func

    hello please help me
    i created follow table function when i use it by "select * from table(customerRequest_list);"
    command i receive this error "cannot perform a DML operation inside a query"
    can you solve this problem?
    CREATE OR REPLACE FUNCTION customerRequest_list(
    p_sendingDate varchar2:=NULL,
    p_requestNumber varchar2:=NULL,
    p_branchCode varchar2:=NULL,
    p_bankCode varchar2:=NULL,
    p_numberOfchekbook varchar2:=NULL,
    p_customerAccountNumber varchar2:=NULL,
    p_customerName varchar2:=NULL,
    p_checkbookCode varchar2:=NULL,
    p_sendingBranchCode varchar2:=NULL,
    p_branchRequestNumber varchar2:=NULL
    RETURN customerRequest_nt
    PIPELINED
    IS
    ob customerRequest_object:=customerRequest_object(
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
    condition varchar2(2000 char):=' WHERE 1=1 ';
    TYPE rectype IS RECORD(
    requestNumber VARCHAR2(32 char),
    branchRequestNumber VARCHAR2(32 char),
    branchCode VARCHAR2(50 char),
    bankCode VARCHAR2(50 char),
    sendingDate VARCHAR2(32 char),
    customerAccountNumber VARCHAR2(50 char),
    customerName VARCHAR2(200 char),
    checkbookCode VARCHAR2(50 char),
    numberOfchekbook NUMBER(2),
    sendingBranchCode VARCHAR2(50 char),
    numberOfIssued NUMBER(2)
    rec rectype;
    dDate date;
    sDate varchar2(25 char);
    TYPE curtype IS REF CURSOR; --RETURN customerRequest%rowtype;
    cur curtype;
    my_branchRequestNumber VARCHAR2(32 char);
    my_branchCode VARCHAR2(50 char);
    my_bankCode VARCHAR2(50 char);
    my_sendingDate date;
    my_customerAccountNumber VARCHAR2(50 char);
    my_checkbookCode VARCHAR2(50 char);
    my_sendingBranchCode VARCHAR2(50 char);
    BEGIN
    IF NOT (regexp_like(p_sendingDate,'^[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}$')
    OR regexp_like(p_sendingDate,'^[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}[[:space:]]{1}[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}$')) THEN
    RAISE_APPLICATION_ERROR(-20000,cbdpkg.get_e_m(-1,5));
    ELSIF (p_sendingDate IS NOT NULL) THEN
    dDate:=TO_DATE(p_sendingDate,'YYYY/MM/DD hh24:mi:ss','nls_calendar=persian');
    dDate:=trunc(dDate);
    sDate:=TO_CHAR(dDate,'YYYY/MM/DD hh24:mi:ss');
    condition:=condition|| ' AND ' || 'sendingDate='||'TO_DATE('''||sDate||''',''YYYY/MM/DD hh24:mi:ss'''||')';
    END IF;
    IF (p_requestNumber IS NOT NULL) AND (cbdpkg.isspace(p_requestNumber)=0) THEN
    condition:=condition|| ' AND ' || ' requestNumber='||p_requestNumber;
    END IF;
    IF (p_bankCode IS NOT NULL) AND (cbdpkg.isspace(p_bankCode)=0) THEN
    condition:=condition|| ' AND ' || ' bankCode='''||p_bankCode||'''';
    END IF;
    IF (p_branchCode IS NOT NULL) AND (cbdpkg.isspace(p_branchCode)=0) THEN
    condition:=condition|| ' AND ' || ' branchCode='''||p_branchCode||'''';
    END IF;
    IF (p_numberOfchekbook IS NOT NULL) AND (cbdpkg.isspace(p_numberOfchekbook)=0) THEN
    condition:=condition|| ' AND ' || ' numberOfchekbook='''||p_numberOfchekbook||'''';
    END IF;
    IF (p_customerAccountNumber IS NOT NULL) AND (cbdpkg.isspace(p_customerAccountNumber)=0) THEN
    condition:=condition|| ' AND ' || ' customerAccountNumber='''||p_customerAccountNumber||'''';
    END IF;
    IF (p_customerName IS NOT NULL) AND (cbdpkg.isspace(p_customerName)=0) THEN
    condition:=condition|| ' AND ' || ' customerName like '''||'%'||p_customerName||'%'||'''';
    END IF;
    IF (p_checkbookCode IS NOT NULL) AND (cbdpkg.isspace(p_checkbookCode)=0) THEN
    condition:=condition|| ' AND ' || ' checkbookCode='''||p_checkbookCode||'''';
    END IF;
    IF (p_sendingBranchCode IS NOT NULL) AND (cbdpkg.isspace(p_sendingBranchCode)=0) THEN
    condition:=condition|| ' AND ' || ' sendingBranchCode='''||p_sendingBranchCode||'''';
    END IF;
    IF (p_branchRequestNumber IS NOT NULL) AND (cbdpkg.isspace(p_branchRequestNumber)=0) THEN
    condition:=condition|| ' AND ' || ' branchRequestNumber='''||p_branchRequestNumber||'''';
    END IF;
    dbms_output.put_line(condition);
    OPEN cur FOR 'SELECT branchRequestNumber,
    branchCode,
    bankCode,
    sendingDate,
    customerAccountNumber ,
    checkbookCode ,
    sendingBranchCode
    FROM customerRequest '|| condition ;
    LOOP
    FETCH cur INTO my_branchRequestNumber,
    my_branchCode,
    my_bankCode,
    my_sendingDate,
    my_customerAccountNumber ,
    my_checkbookCode ,
    my_sendingBranchCode;
    EXIT WHEN (cur%NOTFOUND) OR (cur%NOTFOUND IS NULL);
    BEGIN
    SELECT requestNumber,
    branchRequestNumber,
    branchCode,
    bankCode,
    TO_CHAR(sendingDate,'yyyy/mm/dd','nls_calendar=persian'),
    customerAccountNumber ,
    customerName,
    checkbookCode ,
    numberOfchekbook ,
    sendingBranchCode ,
    numberOfIssued INTO rec FROM customerRequest FOR UPDATE NOWAIT;
    --problem point is this
    EXCEPTION
    when no_data_found then
    null;
    END ;
    ob.requestNumber:=rec.requestNumber ;
    ob.branchRequestNumber:=rec.branchRequestNumber ;
    ob.branchCode:=rec.branchCode ;
    ob.bankCode:=rec.bankCode ;
    ob.sendingDate :=rec.sendingDate;
    ob.customerAccountNumber:=rec.customerAccountNumber ;
    ob.customerName :=rec.customerName;
    ob.checkbookCode :=rec.checkbookCode;
    ob.numberOfchekbook:=rec.numberOfchekbook ;
    ob.sendingBranchCode:=rec.sendingBranchCode ;
    ob.numberOfIssued:=rec.numberOfIssued ;
    PIPE ROW(ob);
    IF (cur%ROWCOUNT>500) THEN
    CLOSE cur;
    RAISE_APPLICATION_ERROR(-20000,cbdpkg.get_e_m(-1,4));
    EXIT;
    END IF;
    END LOOP;
    CLOSE cur;
    RETURN;
    END;

    Now what exactly would be the point of putting a SELECT FOR UPDATE in an autonomous transaction?
    I think OP should start by considering why he has a function with an undesirable side effect in the first place.

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

    I have a Java method which is deployed as a Oracle function.
    This Java method parses a huge XML & populates this data
    into a set of database tables.
    I have to call this Oracle function in a unix shell script using sqlplus.
    Value returned by this function will be used by the shell script to decide
    what to do next.
    I am calling the Oracle Java function as follows in the shell script:
    echo "SELECT XML_TABLES.RUN_XML_LOADER('$P1','$P2','$P3','$P4') FROM DUAL;\n" | sqlplus $DB_USER > $LOG
    This gives error - "ORA-14551: cannot perform a DML operation inside a query".
    If I have to add a AUTONOMOUS_TRANSACTION pragma to this Java function,
    where to I add it considering, that the definition of the function is in a Java class.
    Can we do it in call spec?
    create or replace package XML_TABLES is
    function RUN_XML_LOADER(xmlFile IN VARCHAR2,
    xmlType IN VARCHAR2,
    outputDir IN VARCHAR2,
    logFileDir IN VARCHAR2) RETURN VARCHAR2 AS
    LANGUAGE JAVA NAME 'XmlLoader.run
    (java.lang.String, java.lang.String, java.lang.String, java.lang.String)
    return java.lang.String';
    end XML_TABLES;
    If not is there any other way to acheive this?
    Thanks in advance.
    Sunitha.

    If I have to add a AUTONOMOUS_TRANSACTION pragma to this Java function,You'd have to write a PL/SQL function that calls the JSP. But I would caution you about using that pragma. It does introduce tremendous complexity into processing.
    As I see it you only need a function to return the result code so why not use a procedure with an OUT parameter?
    Cheers, APC
    Of course Yoann's suggestion of using an anonymous block would work too.
    Message was edited by:
    APC

  • Cannot perform a DML operation inside a query

    Dear all,
    I have implemented the "Online Discussion Forum" of Steve Muench (Building Oracle XML Applications,ISBN:1-56592-691-9), and am trying to extend it by logging the queries and timing information inside the ForumSearch.Hits function (http://examples.oreilly.com/orxmlapp/orxmlapp_examples.zip).
    That function is called as follow
    SELECT ForumSearch.Hits(...) as HITS from DUAL
    However, I am getting the ORA-14551.
    That function opens a cursor and performs a select count() on a dynamic SQL, closes the cursor and returns the number of hits found.
    The way i tried to extend it was to simply add the following line after the_cursor was closed.
    insert into LOGGING values(the_count,response);
    However, unfortunately I get the fatal error message
    ORA-14551
    I do understand that one shall not do an INSERT while doing a SELECT on the same table, however in this case I am doing a INSERT on another table ...
    Puzzling enough, if I try to launch a DBMS_JOB for doing that insert I get a ORA-14551 as well.
    I thought that by launching the DBMS_JOB I was going to uncouple the 2 events ...
    Advice is appreciated.
    Luca

    As you discovered, Oracle will not allow you to directly manipulate the database within a select statement. Since the call to dbms_job actually inserts a row in a table, you just moved the DML to a different table. What you need to do is create a seperate procedure to do the logging, and call that in your function. Something like:
    CREATE PROCEDURE logger (p_count IN NUMBER, p_reponse IN VARCHAR2) IS
    PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
       INSERT INTO logging VALUES (p_count, p_response);
       COMMIT;
    END;then, your function would look something like:
    CREATE FUNCTION counter RETURN NUMBER IS
    l_count NUMBER;
    l_response VARCHAR2(10);
    BEGIN
       SELECT COUNT(*) INTO l_rval
       FROM t;
       logger(l_count,l_reponse);
       RETURN l_count;
    END;TTFN
    John

  • Cannot perform DDL DML inside a query

    Hi,
    I am using Tom Kyte's in_list function to create a select list inside a select query in one of the reports in my application, given as follows:
    SELECT col1, col2,...htmldb_item.select_list_from_query(1,NULL,
    '(SELECT column_value d,column_value r FROM the((SELECT CAST
    (mypackage.in_list(''||colon_delimited_list||'') AS
    myTableType) FROM dual) ))' )...FROM A, B WHERE ...
    But the following error appears:
    report error:
    ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML
    ORA-06512: at "FLOWS_020200.WWV_FLOW", line 7002
    ORA-06512: at "FLOWS_020200.WWV_FLOW_UTILITIES", line 113
    ORA-06550: line 1, column 45:
    PLS-00103: Encountered the symbol "(" when expecting one of the following:
    begin case declare exit for goto if loop mod null pragma
    raise return select update while with
    If I replace the query passed to function select_list_from_query, with some simple dummy query, then it works fine.
    Please tell me is there any limitation of its use as this case?
    What may be the possible best solution?
    Regards,
    Amir Aman

    I have tried Scott's parenthesis solution first.
    It is working but with making an additional change, i.e., passing '||''''||colon_delimited_list||''''||' instead just ''||colon_delimited_list||''.
    Denes, I have also seen your examples, really a helpful resource.
    Thank you very much!
    Amir Aman

  • Will Materialized view log reduces the performance of DML statements on the master table

    Hi all,
    I need to refresh a on demand fast refresh Materialized view in Oracle 11GR2. For this purpose I created a Materialized view log on the table (Non partitioned) in which records will be inserted @ rate of 5000/day as follows.
    CREATE MATERIALIZED VIEW LOG ON NOTES NOLOGGING WITH PRIMARY KEY INCLUDING NEW VALUES;
    This table already has 20L records and adding this Mview log will reduce the DML performance on the table ?
    Please guide me on this.

    Having the base table maintain a materialised view log will have an impact on the speed of DML statements - they are doing extra work, which will take extra time. A more sensible question would be to ask whether it will have a significant impact, to which the answer is almost certainly "no".
    5000 records inserted a day is nothing. Adding a view log to the heap really shouldn't cause any trouble at all - but ultimately only your own testing can establish that.

  • Cannot perform DML inside a query

    Hi,
    I have a Function and I am trying to execute an "Insert" statement into the Function. It gives me an error saying that-
    ERROR:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "SCOTT.POP3", line 243
    I then tried adding : PRAGMA AUTONOMOUS_TRANSACTION to the Function and then it gives me an error saying-
    ERROR:
    ORA-06519: active autonomous transaction detected and rolled back
    ORA-06512: at "SCOTT.POP3", line 245
    My code snippet is as follows :
    CREATE OR REPLACE FUNCTION pop3 (
    username VARCHAR2,
    PASSWORD VARCHAR2,
    msgnum NUMBER
    RETURN tstrings PIPELINED
    IS
    --PRAGMA AUTONOMOUS_TRANSACTION;
    pop3_server CONSTANT VARCHAR2 (100) := 'pop.secureserver.net';
    pop3_port CONSTANT NUMBER := 110;
    pop3_ok CONSTANT VARCHAR2 (10) := '+OK';
    e_pop3_error EXCEPTION;
    socket UTL_TCP.connection;
    line VARCHAR2 (30000);
    line2                    VARCHAR2 (30000);
    BYTES INTEGER;
    msg_from varchar2(4000) := '';
    msg_to varchar2(4000) := '';
    msg_date varchar2(4000) := '';
    msg_sub varchar2(4000) := '';
    msg_body clob := NULL;
    hyphen_checker number := 0;
    msg_body_flag number := 0;
    crlf varchar2(2) := chr(13) || chr(10);
    -- send a POP3 command
    -- (we expect each command to respond with a +OK)
    FUNCTION writetopop (command VARCHAR2)
    RETURN VARCHAR2
    IS
    len INTEGER;
    resp VARCHAR2 (30000);
    BEGIN
    len := UTL_TCP.write_line (socket, command);
    UTL_TCP.FLUSH (socket);
    -- using a hack to check the popd response
    len := UTL_TCP.read_line (socket, resp);
    IF SUBSTR (resp, 1, 3) != pop3_ok
    THEN
    RAISE e_pop3_error;
    END IF;
    RETURN (resp);
    END;
    BEGIN
         DBMS_LOB.CREATETEMPORARY(msg_body,true);
    PIPE ROW ('pop3:' || pop3_server || ' port:' || pop3_port);
    -- Just to make sure there are no previously opened connections
    UTL_TCP.close_all_connections;
    -- open a socket connection to the POP3 server
    socket :=
    UTL_TCP.open_connection (remote_host => pop3_server,
    remote_port => pop3_port,
    --tx_timeout => POP3_TIMEOUT,
    CHARSET => 'US7ASCII'
    -- read the server banner/response from the pop3 daemon
    PIPE ROW (UTL_TCP.get_line (socket));
    -- authenticate with the POP3 server using the USER and PASS commands
         LOOP
    EXIT WHEN LENGTH (line) = 1 AND line = '.';
         END LOOP;
         INSERT INTO email (em_issue, em_date, em_from, em_to, em_subject, em_body)
         VALUES(seq_issue.nextval, msg_date, msg_from, msg_to, msg_sub, msg_body);
         DBMS_LOB.freetemporary(msg_body);
         msg_from := '';
         msg_to := '';
         msg_date := '';
         msg_sub := '';
         msg_body := NULL;
         -- close connection
         PIPE ROW ('QUIT');
         PIPE ROW (writetopop ('QUIT'));
         UTL_TCP.close_connection (socket);
    EXCEPTION
    WHEN e_pop3_error
    THEN
    PIPE ROW ('There are no mails !');
    END;
    Message was edited by:
    Monk
    Message was edited by:
    Monk

    See Note:313597.1 on MetaLink

  • Udf in dml statements

    can we select udf which contains dml statements?

    1007109 wrote:
    can we select udf which contains dml statements?
    Excellent question! And one that illustrates just how careful you have to be when you ASK questions or provide answers.
    As with many things Oracle the correct answer is: it depends.
    Yes - you can. And no - you can't. And also, yes - you can!
    ANY one of those answers is correct, depending on what assumptions the questioner is making and/or what assumptions that you, the answerer are making.
    Sometimes a interviewer will ask this old 'trick' question and then get mad at you if you don't give them the answer that they want even if you give them an answer that is CORRECT and even if you explain WHY your answer is correct.
    Some questioners are looking for the answer that Frank gave:
    >
    A function that performs DML (or that calls another sub-program that performs DML) can NOT be used in a SQL statement, such as SELECT.
    >
    If you just read the 'restrictions' section in the Advanced App Developer's guide you might come up with the answer that Frank gave.
    http://docs.oracle.com/cd/E11882_01/appdev.112/e25518/adfns_packages.htm#i1008107
    >
    Restrictions
    When a new SQL statement is run, checks are made to see if it is logically embedded within the execution of a running SQL statement. This occurs if the statement is run from a trigger or from a subprogram that was in turn invoked from the running SQL statement. In these cases, further checks determine if the new SQL statement is safe in the specific context.
    These restrictions are enforced on subprograms:
      A subprogram invoked from a query (SELECT statement) or DML statement cannot end the current transaction, create or rollback to a savepoint, or ALTER the system or session.
      A subprogram invoked from a query or parallelized DML statement cannot run a DML statement or otherwise modify the database.
      A subprogram invoked from a DML statement cannot read or modify the particular table being modified by that DML statement.
    >
    But then if you read the very next sentence in that doc you will see this:
    >
    You can avoid these restrictions if the execution of the new SQL statement is not logically embedded in the context of the running statement. PL/SQL autonomous transactions provide one escape (see "Autonomous Transactions" ).
    >
    And finally if the term DML includes the SELECT statement and the SQL Language doc DOES use it that way
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_1001.htm#i2099257
    >
    The SELECT statement is a limited form of DML statement in that it can only access data in the database. It cannot manipulate data stored in the database, although it can manipulate the accessed data before returning the results of the query.
    >
    That doc also lists statements other than the traditional SELECT, INSERT, UPDATE or DELETE
    >
    Data Manipulation Language (DML) Statements
    Data manipulation language (DML) statements access and manipulate data in existing schema objects. These statements do not implicitly commit the current transaction. The data manipulation language statements are:
    CALL
    DELETE
    EXPLAIN PLAN
    INSERT
    LOCK TABLE
    MERGE
    SELECT
    UPDATE
    >
    So the CORRECT answer is:
    1. No, Frank's answer, if by DML you are only considering INSERT, UPDATE or DELETE statements and you do NOT use the AUTONOMOUS_TRANSACTION pragma.
    2. Yes - if DML includes SELECT (LOCK TABLE also works) - you don't need the pragma. A common use of functions is in LOOKUP operations. These functions can be, but should NOT be, called from a sql statement.
    3. Yes - if the AUTONOMOUS_TRANSACTION pragma is used. This is NOT recommended.
    Try some of the following code examples
    >
    create or replace function get_dname(p_deptno IN number) return varchar2 is
    v_dname dept.dname%type;
    begin
      select dname into v_dname from dept where deptno = p_deptno;
      return v_dname;
    end;
    select empno, ename, deptno, get_dname(deptno) from emp e
    where rownum < 4
    EMPNO,ENAME,DEPTNO,GET_DNAME(DEPTNO)
    7369,SMITH,20,RESEARCH*
    7499,ALLEN,30,SALES**
    7521,WARD,30,SALES**
    create or replace function get_dname1(p_deptno IN number) return varchar2 is
    v_dname dept.dname%type;
    begin
      update dept set dname = dname || '*' where deptno = p_deptno;
      select dname into v_dname from dept where deptno = p_deptno;
      return v_dname;
    end;
    select empno, ename, deptno, get_dname1(deptno) from emp e
    where rownum < 4
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "SCOTT.GET_DNAME1", line 4
    create or replace function get_dname2(p_deptno IN number) return varchar2 is
    PRAGMA AUTONOMOUS_TRANSACTION;
    v_dname dept.dname%type;
    begin
      update dept set dname = dname || '*' where deptno = p_deptno;
      commit;
      select dname into v_dname from dept where deptno = p_deptno;
      return v_dname;
    end;
    select empno, ename, deptno, get_dname2(deptno) from emp e
    where rownum < 4
    EMPNO,ENAME,DEPTNO,GET_DNAME2(DEPTNO)
    7369,SMITH,20,RESEARCH**
    7499,ALLEN,30,SALES***
    7521,WARD,30,SALES****
    create or replace function get_dname3(p_deptno IN number) return varchar2 is
    v_dname dept.dname%type;
    begin
      lock table emp_copy in share mode;
      select dname into v_dname from dept where deptno = p_deptno;
      return v_dname;
    end;
    select empno, ename, deptno, get_dname3(deptno) from emp e
    where rownum < 4
    EMPNO,ENAME,DEPTNO,GET_DNAME2(DEPTNO)
    7369,SMITH,20,RESEARCH**
    7499,ALLEN,30,SALES***
    7521,WARD,30,SALES****
    >

  • Calling PLSQL from DML Statements

    hello everybody,
    i have just a minor "problem". (this confuses me because of the 1z0-147 programming plsql exam)
    Oracle Documentation (10G R2 Application Developer's Guide - Fundamentals - 7 Coding PL/SQL Procedures and Packages) says:
    The following restrictions are enforced on subprograms:
    => A subprogram called from a DML statement may not read or modify the particular table being modified by that DML statement.
    i did some tests but they resulted not in an error (mutating table exception or whatever); so maybe Oracle's documentation is inaccurate here ?
    here is an example which works fine (10G R2):
    drop table ttestuh
    create table ttestuh ( s varchar2(19));
    insert into ttestuh values ( 'Oracle' );
    create or replace function getVal return varchar2 is
    retval ttestuh.s%type;
    begin
    select s into retval from ttestuh where rownum <2;
    return retval;
    end;
    create or replace function getVal2 return varchar2 is
    begin
    insert into ttestuh values ( 'ASDF');
    return 'ASDF';
    end;
    insert into ttestuh values ( getval );
    insert into ttestuh values ( getval2 );
    commit
    select * from ttestuh
    rows:
    Oracle
    Oracle
    ASDF
    ASDFit seems to be that even DML-doing PLSQL calls are allowed when called from an insert statement on the same table.
    (for update statements an mutating table exception is raised)
    thanks in advance for your opinions
    ulli

    I believe what docs are saying is correct. And also what you are seeing is correct. There may be a small issue in understanding the impact.
    SQL>  create or replace function fun1
      2   return number
      3   is
      4   begin
      5   delete test;
      6   end;
      7  .
    SQL>  create or replace function fun1
      2   return number
      3   is
      4   begin
      5   update test set owner='amn';
      6  end;
      7  /
    Function created.
    SQL> select fun1 from test;
    select fun1 from test
    ERROR at line 1:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "AMAN.FUN1", line 5
    SQL>  create or replace function fun1
      2   return number
      3   is
      4   begin
      5  insert into test(owner) values('new');
      6  end;
      7  /
    Function created.
    SQL> select fun1 from test;
    select fun1 from test
    ERROR at line 1:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "AMAN.FUN1", line 5
    SQL>The mutating error means that the data that you are trying to change is also read by the same session and this means, Oralce can't give a "dirty read" to you. That's why in the case of Update, the mutating error is returned. The insert statement is inserting new data which anyways your select can't see as long as its not committed and with it you haven't changed the existing one that's why its allowed.
    SQL> create or replace function fun1
      2  return varchar2
      3  is
      4  a varchar2(10);
      5  begin
      6  select owner into a from test where rownum<=1;
      7  end;
      8  /
    Function created.
    SQL> update test owner=fun1;
    update test owner=fun1
    ERROR at line 1:
    ORA-00971: missing SET keyword
    SQL> update test set owner=fun1;
    update test set owner=fun1
    ERROR at line 1:
    ORA-04091: table AMAN.TEST is mutating, trigger/function may not see it
    ORA-06512: at "AMAN.FUN1", line 6
    SQL>HTH
    Aman....

  • ORA-14551 Call to a proc that performs INSERT from within a FUNCTION

    Anyone,
    I have written a number of functions that serve to convert an old_value to a new_value. But there are some times when the conversion fails. I wish to log those exceptions into a table and move on. I undertand FUNCTIONS are not to insert data but that is why I created a PROC and want to be able to call it from within the FUNCTIONs.
    Is there any way to get around the ORA-14551 error?
    Here is what I have:
    CREATE OR REPLACE FUNCTION MAXIMO.fcn_get_worktype
      ( worktype_IN IN varchar2,
        siteid_IN IN varchar2)
      RETURN  varchar2 IS
      var_worktype varchar2(5);
    BEGIN
         IF siteid_IN IN ('ML','OK','BE') THEN
            BEGIN
                 SELECT NVL(NEW_WTYPE,NULL)
                INTO var_worktype
                 FROM MAXIMO.MAX411_WBK_WORKTYPE$
                 WHERE OLD_WTYPE = worktype_IN;
            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    MAXIMO.SP_CONVERSION_EXCEPTION('WORKTYPE', 'WORKTYPE', worktype_IN, siteid_IN);
                    var_worktype := 'XX';
                     RETURN var_worktype;
            END;
        END IF;
        RETURN var_worktype ;
    END;And here is my PROC:
    CREATE OR REPLACE PROCEDURE MAXIMO.sp_conversion_exception (table_IN IN VARCHAR2, column_IN IN VARCHAR2, value_IN IN VARCHAR2, tab_ID IN VARCHAR2)
    IS
    BEGIN
        INSERT INTO MAXIMO.MAX411_CONVERSION_EXCEPTIONS
             ( TABLE_NAME,
             COLUMN_NAME,
             VALUE ,
              TAB_ID)
        VALUES
             ( table_IN,
             column_IN,
             value_IN,
              tab_ID );
        EXCEPTION
            /* If value was already recorded then do not record again, Key is on all three columns */
            WHEN DUP_VAL_ON_INDEX
                THEN
                    NULL;
        COMMIT;
    END;Can anyone shed any light on how I can stop getting:
    ERROR at line 1:
    ORA-14551: cannot perform a DML operation inside a query
    ORA-06512: at "MAXIMO.SP_CONVERSION_EXCEPTION", line 5
    ORA-06512: at "MAXIMO.FCN_GET_WORKTYPE", line 16
    ORA-01403: no data found
    ORA-06512: at "MAXIMO.LOAD_PM", line 181
    ORA-06512: at line 1
    Thanks in advance for any help I can get...
    Miller

    You need a PRAGMA AUTONOMOUS_TRANSACTION in your exception procedure, eg:
    CREATE OR REPLACE PROCEDURE MAXIMO.sp_conversion_exception (table_IN IN VARCHAR2, column_IN IN VARCHAR2, value_IN IN VARCHAR2, tab_ID IN VARCHAR2)
    IS
      PRAGMA AUTONOMOUS_TRANSACTION
    BEGIN
      ...

  • Probelm regarding function ( can not perform dml openration inside a query

    hi all,
    i have created on function which is used for delete rows and insert new rows ( records) but when i run i got
    ora-14551 -can not perform a DML operation inside a query
    0ra-06512 - fn_brok_upfdate_dtltbl , line no 21
    this error
    following are my code please help me
    CREATE OR REPLACE FUNCTION Fn_Brok_Update_Dtltbl
    (fnBROK_TYPE VARCHAR2,fnSLAB_ID VARCHAR2, fnINV_TYPE_CODE VARCHAR2)
    RETURN NUMBER
    AS
    fnCOUNT      PLS_INTEGER;
    fnTBLNAME     VARCHAR2(40);
    fnTMPINV_TYPE_CODE VARCHAR2(1000);
    fnTMPINVCODE       VARCHAR2(50);
    fnMYQUERY          VARCHAR2(4000);
    BEGIN
          IF UPPER(fnBROK_TYPE) = 'UPFRONT' THEN
          fnTBLNAME :='UPFRONT_RECD_DTL';
          END IF ;
        IF fnSLAB_ID IS NOT NULL AND fnINV_TYPE_CODE IS NOT NULL THEN
        fnTMPINV_TYPE_CODE := fnINV_TYPE_CODE ;
                 fnMYQUERY := 'SELECT COUNT(*) FROM '||fnTBLNAME||' WHERE SLAB_ID = '''||fnSLAB_ID||'''' ;
                 EXECUTE IMMEDIATE fnMYQUERY INTO fnCOUNT  ;
              IF fnCOUNT > 0 THEN
                    DELETE FROM UPFRONT_RECD_DTL WHERE slab_id = fnslab_id ;
              END IF ;
              WHILE fnTMPINV_TYPE_CODE IS NOT NULL
                    LOOP
                        SELECT  CASE WHEN INSTR(fnTMPINV_TYPE_CODE,',')= 0 THEN fnTMPINV_TYPE_CODE
                        ELSE Strsplit(fnTMPINV_TYPE_CODE,1,INSTR(fnTMPINV_TYPE_CODE,',') - 1  ) END  INTO fnTMPINVCODE FROM DUAL ;
                        fnMYQUERY := 'INSERT INTO '||fnTBLNAME||'(SLAB_ID, INV_TYPE_CODE)
                                VALUES ('''||fnSLAB_ID||''','||fnTMPINVCODE||') ' ;
                        EXECUTE IMMEDIATE fnMYQUERY ;
                        SELECT CASE WHEN INSTR(fnTMPINV_TYPE_CODE,',')= 0 THEN NULL
                        ELSE Strsplit(fnTMPINV_TYPE_CODE,INSTR(fnTMPINV_TYPE_CODE,',')+ 1 , LENGTH(fnTMPINV_TYPE_CODE)) END
                        INTO fnTMPINV_TYPE_CODE FROM DUAL ;
                   END LOOP ;
       fnCOUNT := 1 ;
       RETURN fnCOUNT ;
       END IF ;
    END ;
    [\pre]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    How did you call this function ? from SQL Statement ? Then No way .. . .
    ORA-14551: cannot perform a DML operation inside a query
    Cause: DML operation like insert, update, delete or select-for-update cannot be performed inside a query or under a PDML slave.
    Action: Ensure that the offending DML operation is not performed or use an autonomous transaction to perform the DML operation within the query or PDML slave

  • ORA-20001: Oracle error -20001: ORA-20001: Oracle error -14552: ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML has been detected in fnd_global.set_nls.set_parameter('NLS_LANGUAGE','AMERICAN'). has been detected in fnd_global.set

    ORA-20001: Oracle error -20001: ORA-20001: Oracle error -14552: ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML has been detected in fnd_global.set_nls.set_parameter('NLS_LANGUAGE','AMERICAN'). has been detected in fnd_global.set_nls.
    this error generates everytime when i run a apex application
    i did call the package in session state with  dbms_session.set_nls('NLS_LANGUAGE','AMERICAN'); but still it shows the error in the report any solution please

    Hi Amrit
    Can you put the FND_GLOBAL calls in a pre map trigger/process rather than as part of the map?
    Cheers
    David

  • Cannot perform dml operation inside a query

    I have created a function which does some dml opration.
    when I use it in a through a transformation operator, and execute the map,
    it throws the following error:
    cannot perform dml operation inside a query
    how to handle this?

    Hi,
    if you want to execute the dml within a mapping, use the pre or post mapping procress operator. Or use a sql*plus activity in the process flow.
    Regards,
    Carsten.

  • ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML;;

    Hi all,
    I have a problem!
    I have one PL SQL function doing an Update and insert into some oracle tables, this function return one number (0 or 1 or 2).
    I'm excuting this query from one PHP program to call the function :
    SELECT SALE.DB_FUNC_ASSOCIATE_TB2U('[email protected]',123)
    FROM dual;
    the problem that I have these errors :
    ERROR DB_FUNC_ASSOCIATE_TB2U Level 1 : ORA-01403: no data found
    ERROR DB_FUNC_ASSOCIATE_TB2U : ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML
    there's a mean that i can do to call this function from PHP without getting this Error such as I tested this function using the script below and it works fine:
    declare
    a number;
    begin
    a:= SALE.DB_FUNC_ASSOCIATE_TB2U('[email protected]',123) ;
    end;
    Please help me :)
    regards,

    Sure, it works in the sense that you would no longer get an error.
    Creating this as an autonomous transaction, however, is going to blow your transactional integrity. You've now got code that is being called as the result of a SELECT operation (something that everyone that comes after you will assume is read-only) that commits outside of the current transaction scope. That is almost always a bad thing.
    In addition, having a WHEN OTHERS without a RAISE is almost always a bug. You're completely hiding the error message plus you're discarding proper exception handling in order to provide a return code and assuming that the caller is going to check the status. Relying on return status codes is normally a bad idea because it's very easy on the client side to forget to check the return code. Even if the client were to check the return code, it has no idea what went wrong, so it can't even log the root cause of the error.
    Justin

Maybe you are looking for

  • ACS 5.3 Shell Command Set

    Hi all, Currently i deploy a ACS 5.3 at customer site. The issue i face currently is some command sets no able to deny. Example like below: i want to deny the AD user with priviledge level 15 to change the enable secret password and delete the enable

  • How can I view more than two pages at a time?

    How can I view more man two pages at a time? As a muscian reading digital sheets on Reader, being able to veiw as many as 3 to 5 pages at a time would be more helpful. Also, if it was possible to view any number of pages at time such as pages 1 and 2

  • Having Trouble installing Windows XP on Macbook

    Hi! I've been trying to install Windows XP Service Pack 2 for the past 2 weeks now (first via VM ware fusion then with boot camp) but i keep getting the same stop error message with both: "A problem has been detected and Windows has been shut down to

  • Wave Studio says "device doesn't support this format"

    ?Wave Studio says "device doesn't support this format"'I have a X-Fi Extreme Music card I'm using in Windows 7 (x64). I haven't had any issues with the card or the software until I installed a codec pack: Shark Windows 7 Codec pack. It worked OK, but

  • Updating Master Data

    HI all, I have full upload for the master data from CRM and R3. And after that i intialized the delta too thru a process chain. No what happens if user creates a new material on CRM and how its gonna be reflected on the BW. Is it that, when a new mat