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....

Similar Messages

  • Calling PLSQL Stored Procedure From HTML Form Submit Button

    Hi there,
    I am having a little difficulty with calling a stored procedure using an html form button. Here is the code I have right now...
    HTP.PRINT('<form action=ZWGKERCF.P_confdelete>');
    HTP.PRINT('<input type=''submit'' value='' Yes '' onClick=''document.getElementById("mypopup").style.display="none"''>');
    HTP.PRINT('</form></div>');Here is the issue - I need to find a way to pass variables to this stored procedure so it know what data to operate on. This stored procedure will delete data for a specific database record and I must pass three variables to this procedure to make it work.
    Lets call them class_number, term, conf These three variables will be passed and the data will be deleted and the person will see a confirmation screen once the delete query has been executed.
    So ideally I would want: ZWGKERCF.P_confdelete(class_number, term, conf) and then the stored procedure would handle the rest!
    Seems pretty simple but I am not sure how to make this happen... My thoughts were:
    Pass the data to this html form (the three fields I need) in hidden variables. Then somehow pass these using POST method to the procedure and read using GET?
    Can someone clarify what the best way to do this is? I have a feeling its something small I am missing - but I would really like some expert insight :-)
    Thanks so much in advance!
    - Jeff

    795018 wrote:
    I am having a little difficulty with calling a stored procedure using an html form button. Here is the code I have right now...
    HTP.PRINT('<form action=ZWGKERCF.P_confdelete>');
    HTP.PRINT('<input type=''submit'' value='' Yes '' onClick=''document.getElementById("mypopup").style.display="none"''>');
    HTP.PRINT('</form></div>');Here is the issue - I need to find a way to pass variables to this stored procedure so it know what data to operate on. This stored procedure will delete data for a specific database record and I must pass three variables to this procedure to make it work. The browser generates a POST or a GET for that form action, that includes all the fields defined in that form. Let's say you define HTML text input fields name and surname for the form. The URL generated for that form's submission will be:
    http://../ZWGKERCF.P_confdelete?name=value1&surname=value2The browser therefore submits the values of the form as part of the URL.
    The web server receives this. It sees that the base URL (aka location) is serviced by Oracle's mod_plsql. It passes the URL to this module. This module builds a PL/SQL block and makes the call to Oracle. If we ignore the additional calls it makes (setting up an OWA environment for that Oracle session), this is how the call to Oracle basically looks like:
    begin
      ZWGKERCF.P_confdelete( name=> :value1, surname =>  :value2 );
    end;Thus the PL/SQL web enabled procedure gets all the input fields from the HTML form, via its parameter signature. As you can define parameter values with defaults, you can support variable parameter calls. For example, let's say our procedure also have a birthDate parameter that is default null. The above call will still work (from a HTML form that does not have a date field). And so will the following URL and call that includes a birth date:
    URL:
    http://../ZWGKERCF.P_confdelete?name=value1&surname=value2&birthdate=2000/01/01
    PL/SQL call:
    begin
      ZWGKERCF.P_confdelete( name=> :value1, surname =>  :value2, birthdate => :value3 );
    end;There is also another call method you can use - the flexible 2 parameter interface. In this case the PL/SQL procedure name in the URL is suffixed with an exclamation mark. This instructs the mod_plsql module to put all input field names it received from the web browser into a string array. And put all the values for those fields in another string array. Then it calls your procedure with these arrays as input.
    Your procedure therefore has a fixed parameter signature. Two parameters only. Both are string arrays.
    The advantage of this method is that your procedure can dynamically deal with the web browser's input - any number of fields. The procedure's signature no longer needs to match the HTML form's signature.
    You can also defined RESTful mod_plsql calls to PL/SQL. In which case the call format from the web browser looks different and is handled differently by mod_plsql.
    All this (and more) is detailed in the Oracle manuals dealing with mod_plsql - have a search via http://tahiti.oracle.com (Oracle Documentation Portal) for the relevant manuals for the Oracle version you are using.
    Alternatively, simply download and install Oracle Apex (Application Express). This is a web development and run-time framework and do all the complexities for you - including web state management, optimistic locking, security and so on.

  • Calling PL/SQL code from Select statement

    Hi
    I have a PL/SQL function to calculate a value.
    create or replace procedure "SR_GROSS_MARGIN"
    (netsales IN NUMBER,
    margin IN NUMBER,
    GM OUT NUMBER)
    is
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    END;How do I call this from a SELECT statement?
    Regards
    Adam

    here you go:
    create or replace function SR_GROSS_MARGIN
    (netsales IN NUMBER,
    margin IN NUMBER)
    return number
    is
    gm number;
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    return gm;
    END;then you can:
    select gm(2500,20) from dual;

  • Someone calling you from outside the United States

    If my friend calls me from Australia and they use either a land line or a cell phone. Am I going to get charged extra at all even if my plan is older on my cell phone? I only get 500 shared minutes a month at the moment for all types of minutes except M2M. Also what type of minutes am I going to eat (i.e: my everyday plan minutes or roaming) if I am currently in my home state in the US? Also is it going to show up on the bill differently then normal than any other type of call?
    Feedback and such would be much appreciated! Thanks

    Suemac wrote:
    If my friend calls me from Australia and they use either a land line or a cell phone. Am I going to get charged extra at all even if my plan is older on my cell phone? I only get 500 shared minutes a month at the moment for all types of minutes except M2M. Also what type of minutes am I going to eat (i.e: my everyday plan minutes or roaming) if I am currently in my home state in the US? Also is it going to show up on the bill differently then normal than any other type of call?
    Feedback and such would be much appreciated! Thanks
    As long as you are in the US when you receive their call, you will not get charged anything extra for receiving this call.

  • Please can someone help,i bought an iphone from the states,i put a sim in ,i could make phone call,but i couldnt surf ,it seems that something is wrong and when i click on wireless nozhing happen,

    please someone help i bought an iphone4 from the states, i put the sim card in, icould make a phone call,thats ok but i couldnt open the opera,and when i click on wireless on buttom nothings happen.

    Are you talking about an unlocked version?  If an iPhone is locked to AT&T in the U.S., anyting is fair game outside of AT&T's network. (You can always use WiFi, though.)

  • Call dll from plsql

    Hi
    I am trying to connect an external program(DLL) from pl/sql.
    for the practice i trying to call "GetComputerName" from kernel32.dll.
    i made the following step
    1. Creating a library .
    2. Creating a package .
    3. Set the listener.ora
    4. Set the tnsnames.ora.
    After solving the problame with the settings of the listener.ora
    I testing the function , it run with no error but return 'null' .
    I thing the problems is with the "parameter"
    I do not know of haw to send them .
    ----my pak-------------------------------
    CREATE OR REPLACE Package Body K32 As
    Function GetComputerNameK(lpBuffer out varchar2,nSize in out long )Return long Is
    External
    Library Sys.Kernel32 Name "GetComputerNameW"
    Language c
              WITH CONTEXT
    PARAMETERS (
    CONTEXT,
    --lpBuffer STRING,
              lpBuffer BY REFERENCE,
              nSize BY REFERENCE,
              --nSize INDICATOR short,*/
              RETURN INDICATOR--,
         /*     RETURN short */);
    End K32;

    Let me explain a bit what a VFP DLL is for:
    You can only build one type of DLL, an OLE COM Server DLL. It'll mainly contain OLEPUBLIC class definitions, which are then usable in other programming languages. In any programming language capable to make use of OLE COM Server classes.
    If your project is named mydll.pjx and a prg or vcx contains a class myclass, the final DLL will have the OLE class "mydll.myclass" in it.
    This is overhead, if you use this in VFP. If you want to modularize your application, then create several EXE or build as APP. You can DO some.prg IN some.APP or you can create an object o = NEWOBJECT("myclass","myclasslib.vcx","myapp.app")
    to refer to a class inside a vcx compiled and build together with other project files into an app file.
    But any separation you do complicates the code use. A DLL is only needed, if some other programming language needs to use your VFP code. And that other programming language has to be able to instanciate classes.
    You don't need this, even if other programming languages would be involved. You can compile an EXE and that can be run. If needed with parameters. That's typically much simpler and could even be used by DOS batch files either using the DOS start command
    or directly your.EXE as man DOS commands also are merely EXE files.
    Bye, Olaf.
    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

  • Writing own dml statements in jdeveloper

    i need to write my own dml statement in a jdevelopers application. like on saving a purchase form i would like to update stock table as well. Some one told me to write code in DoDML procedure but how ? i mean what statement should i write there ?

    lot of possibilities but easy way is to call PL/SQL code i guess you are coming from FORMS background so it would be easy and understandable for you
    You can expose PL/SQL package as web-services also you can call stored procedures and functions in ADF
    here is the example http://baigsorcl.blogspot.com/2010/05/calling-plsql-procedure-and-function-in.html
    doDML() will work at entity level you can call your PL/SQL at doDML as well or as call its method binding

  • PLS-00435: DML statement without BULK In-BIND cannot be used

    My requirement
    I am dynamically creating a staging table my_stg, and then populate it. Seems simple, but not sure why i get this error,
    create table gtest4(myid varchar2(10), mykey varchar2(10));
    create table gtest5(myid varchar2(10), mykey varchar2(10));
    insert into gtest4 values(1,3);
    insert into gtest4 values(2,7);
    insert into gtest5 values(5,3);
    insert into gtest5 values (1,7);
    commit;
    /* Formatted on 2012/01/27 17:52 (Formatter Plus v4.8.8) */
    CREATE OR REPLACE PROCEDURE px
    IS
    TYPE rectype IS RECORD (
    myid VARCHAR2 (100),
    mykey VARCHAR2 (100)
    TYPE tabtype IS TABLE OF rectype
    INDEX BY BINARY_INTEGER;
    rec tabtype;
    cur sys_refcursor;
    BEGIN
    EXECUTE IMMEDIATE 'create table my_stg(myid varchar2(100), mykey varchar2(100)) ';
    OPEN cur FOR 'select a.myid, b.mykey
    from gtest4 a, gtest5 b
    where a.mykey = b.mykey';
    LOOP
    FETCH cur
    BULK COLLECT INTO rec LIMIT 500;
    FORALL i IN 1 .. rec.COUNT
    EXECUTE IMMEDIATE 'insert into my_stg(myid, mykey) values (rec(i).myid,
    rec(i).mykey)';
    EXIT WHEN cur%NOTFOUND;
    END LOOP;
    END;
    I compile the above proc, and get
    PLS-00435: DML statement without BULK In-BIND cannot be used
    the reason I do insert in execute immediate is because the table my_stg does not exist, it is created on the fly

    I tried the below, used plsql table variables instead of record type
    CREATE OR REPLACE PROCEDURE px
    IS
    TYPE rectype IS RECORD (
    myid VARCHAR2 (100),
    mykey VARCHAR2 (100)
    TYPE tabtype1 IS TABLE OF varchar2(100)
    INDEX BY BINARY_INTEGER;
    TYPE tabtype2 IS TABLE OF varchar2(100)
    INDEX BY BINARY_INTEGER;
    rec1 tabtype1;
    rec2 tabtype2;
    cur sys_refcursor;
    BEGIN
    EXECUTE IMMEDIATE 'create table my_stg(myid varchar2(100), mykey varchar2(100)) ';
    OPEN cur FOR 'select a.myid, b.mykey
    from gtest4 a, gtest5 b
    where a.mykey = b.mykey';
    LOOP
    FETCH cur
    BULK COLLECT INTO rec1, rec2 LIMIT 500;
    FORALL i IN 1 .. rec.COUNT
    execute immediate 'insert into my_stg(myid, mykey) values (:1,:2)
    using rec1(i).myid, rec2(i).mykey;
    EXIT WHEN cur%NOTFOUND;
    END LOOP;
    END;
    I get error
    PLS-00103: Encountered the symbol "insert into my_stg(myi
    mykey) values (:1,:2)
    using rec1(i).myi" when expecting one of the following:
    ( - + case mod new not null <an identifier>
    <a double-quoted delimited-identifier> <a bind variable>
    count current exists max min prior sql stddev sum varianc
    execute forall merge time timestamp interval date
    <a string literal with character set specification>
    <a number> <a single-quoted SQL string> pipe
    <an alternatively-quoted string literal
    Please help

  • How to call Plsql function in discoverer

    Hello,
    Can anyone tell me how to call plsql fuunction in Oracle Business Intelligence Desktop Discoverer?
    what i was doing here is i am able to register my function in Administration but i dont know when i need to make call and where i need to pass parameters to make call that function.
    its lil urgent, can any of you guys send me the instructions step by step
    Thanks in advance!!
    sid

    Thanks for your reply Michael,some how i found that make call from calculation , but the problem here is i was not able to pass parameter thru function.please find the scenario below,
    case(1) i have function without parameters, it does some DML operations inside that and it is working fine.when i refresh i see tables getting populated.
    case(2)I have another function as shown below with parameters(input) but i was getting error (One of The function arguments has an incorrect datatype:POST_SAVE_DOCUMENT)
    my function is like this:
    create or replace function POST_SAVE_DOCUMENT
    (P_WORKBOOKOWNER in VARCHAR2,
    P_WORKBOOK in VARCHAR2,
    P_WORKSHEET in VARCHAR2,
    P_SEQUENCE in number,
    P_SQL_SEGMENT in VARCHAR2
    return number AS
    BEGIN
    delete from my_sql;
    insert into my_sql
    ( WORKBOOKOWNER,
    WORKBOOK,
    WORKSHEET,
    SEG_SEQUENCE,
    SQL_SEGMENT)
    VALUES ( P_WORKBOOKOWNER,
    P_WORKBOOK,
    P_WORKSHEET,
    P_SEQUENCE,
    P_SQL_SEGMENT
    return 1;
    end;
    i dont see anything wrong here with in this function, but some how i have a problem with parameter.
    Can you give some idea or solution to get rid of this problem.
    Finally ..i did not see anything under item radio button (The recommended way now is to click the Items radio button, find an item that you want to pass to a parameter, then on the right-hand side highlight the placeholder for the parameter, then finally you click the Paste button. You repeat this for all the parameters and then click OK to finish the calculation.
    Thanks in adavance!!
    Thanks
    S!D
    Edited by: user12861418 on Apr 28, 2010 2:01 PM

  • Getting session hang When calling Function from SQL query

    Hi All,
    I am using Oracle 8.1.7.4.0. I have a fucntion in a Package and I am calling it from the SQL query. When I am firing the query my oracle session is going to hang position and I am not able to any thing. I have to kill the session.
    But this same thing is working fine in Oracle 9.i.
    There are no out parameter and no DML, DDL and DCL statement in this fucntion.
    Could you please get back me what is the problem on it.
    Regards
    SUN

    Check why your session hangs.
    Just a few ideas:
    * Blocking locks?
    * Endless loop?
    * Performance (maybe it is just slow in orac8i and you have to wait a bit longer). Check the execution plans of the SQL statements in your function.
    * Don't use a function, but direct SQL, it is faster in both versions.

  • Constructing Database Update DML Statements

    This is a very common problem I am sure but want to know how others handle it. I am creating a web application via Servlets and JSPs. I am working with Oracle 10g.
    Here's the problem/question.
    When I present an html form to a user to update an existing record, how do I know what has changed in the record to create the dml statement? I could just update the entire record with the values supplied via the POST data on submit but that does not seem right since maybe only 1 out of 10 fields actually changed. This would also write needless audit and logging information to the database. I have thought about comparing the Request parameters sent with the post with the original java bean used to populate the form in the first place by adding it back to the request as an attribute. Is this the only way to do it or is there a better way?
    Thanks everyone,
    -Brian

    What database drivers are you using? I recall there being some bug in
    sp2 that caused delayed transaction commits for a very specific
    combination of transaction settings and database drivers.
    I think it was third-party type II Oracle drivers and local
    transactions, but I'm not sure.
    In any case, I'd contact support as there is a patch available.
    David
    Gurjit wrote:
    Hi all,
    I have posted something of this sort earlier too. The problem is that
    the database is not reflecting what has been updated using queries from the
    application.
    THe structure of the application is as follows.
    DB = Oracel 8.1.6
    Web Server = iPlanet 4.0
    App Server = iPlanet 6.0 sp2
    The database is connected to via the EJB's. No Database transactions are
    being used except for Bean transactions which are container managed. The
    setAutoCommit flag is set to true. Each query is a transaction. The jsp's
    call these ejb's through wrapper classes. As stated earlier the database
    updates (Inserts, update, delete statements) are not getting reflected in
    the database immediately. The updates happen after a gap of about 7-10
    minutes. Why is this sort of behaviour coming up. This is almost like batch
    updates happening on the database. Is there a setting in IPlanet for
    removing this kind of problem.
    Regards,
    Gurjit

  • 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****
    >

  • Regarding Restrictions on calling functions from sql expressions

    Hi all,
    While going through the functions of oracle 9i plsql documentation i came across restrictions on calling a function, i that i did not understand the following statement. can anyone explain me with an example. Please.........
    Functions called from SQL statements cannot contain statements that end the transactions.
    Regards,
    Sri Ram.

    Some where from google
    http://www.ucertify.com/article/what-are-the-restrictions-on-a-user-defined-function-that-is-called-from-a-sql-expression.html
    >
    •The function cannot contain statements that end the transaction. For example, the function cannot contain transaction control statements (such as COMMIT), session control statements (such as SET ROLE), or system control statements (such as ALTER SYSTEM). Also, it cannot contain DDL statements (such as CREATE) because they are followed by an automatic COMMIT.

  • DML statements in Function

    Hi,
    I have small question
    Why we should not written DML statements in Function ?
    Can any one explain that
    Thanks...

    971822 wrote:
    Hi,
    I have small question
    Why we should not written DML statements in Function ?
    Can any one explain thatThe answer is: "it depends".
    Functions are used for many many different purposes, and it is the purpose that determines whether it's a good idea to write DML statements in a function.
    As already mentioned, there are the purity rules, that basically say "if you're going to be calling the function from queries etc. then it cannot do DML, otherwise you'll get an exception"
    But there are also functions that are intended to be used within PL/SQL code for other purposes. It may be that you need something that updates some data and returns a result from that updated data, or it returns how much data was effected, in which case it's perfectly ok to have a function do some DML.
    As long as the code is well modularised and makes sense for the purpose for which it is intended, then you are doing the right thing. Where it goes wrong is when people lack design in their applications, and try to write 'generic' functions to be used in PL/SQL or SQL, or fail to modularise code so a function is doing the task of several things based on some parameters etc. when really each task should seperated to it's own functions/procedures etc.
    So we cannot just say that we "should not" write DML inside functions; but we can say that there are design considerations (or lack of) when it would be bad practice to do so.

  • If we use DML statement in function then that function can be used inside s

    if we use DML statement in function then that function can be used inside select query or any DML query?

    select f from t2;I think you meant to query t1.
    It works if the function is an autonomous transaction:
    create or replace function f return number
    is
    PRAGMA AUTONOMOUS_TRANSACTION;
    begin
        update t1 set c=2;
        commit;
        return 1;
    end;
    select f from t1But as Billy said why would you want to do DML this way. And this is not the way autonomous procedures should be used either.
    An an answer to an interview question though nothing wrong with it.

Maybe you are looking for

  • How do I download the new itunes software on my iphone?

    When plug my iphone into my laptop and load up itunes a dialog box appears saying I need to download a new version of itunes onto my iphone. I have no idea where to find this download and I have not received any prompt on my iphone to say itunes need

  • HT204406 Problem with iTunes Match and songs purchased under an old ID

    I have songs purchased thru iTunes in 2007 under an old and no longer used ID.  They all appear in the list of purchased music but will not match and appear as DRM files on my Mac (hence the error).  How do I get this resolved?

  • Cisco Secure ACS 5.4/Monitoring and Report Viewer - SNMP Settings

    Hello Everyone. I hope this is the right forum for my question. We just purchased 8 1121 ACS 5.4 appliances. I have some familiarity with the older 1113 and 1120 appliances running ACS 4.2. So I have a lot to learn. Right now I'm trying to understand

  • Uploading pics from iPhone4

    Whenever I have plugged in my iPhone through USB to charge and upload photos I have taken on my iPhone, my MacBook Pro automatically launches iPhoto and asks me if I want to upload the photos but now it seems as if my MacBook Pro is not recognizing m

  • BI 4.0 - Web page at logon

    Hi all, in SAP BI 4.0 platform I need to link webpages at user login as done in XI 3.1 by Personal Infoview: dashboard properties - web address This webpage is useful to link a URL for BO report launch How can I do?