Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Calling anonymous plsql blocks

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling anonymous plsql blocks

    We have one requirement in which we have one plsql stored procedure which does certain data maintenance task like ADD/Drop partition, stats collection etc in Oracle database. It takes table name,owner_name..etc as input. It does have cursor loops inside it to perform the respective task, say for. e.g. rebuilding all global indexes after partition drop for the table. And output returned as success/failure etc.

    Now we are planning to implement these code/functionality for multiple databases from a standalone application which will use Java. So basically we don't want to create separate procedure/objects on each of the target database to do the same task, rather, we want to achieve the functionality by just giving call to a anonymous block which will perform all the task same as that of procedure.

    We are trying to see the easiest possible way to achieve this, and I see below blog which suggest some way similar to Oracle "execute immediate" code i.e. putting all the code in a concatenated string and then executing it. Not sure if any limitation exists for the cursor query or collection type execution while calling the anonymous block in such a way. But it looks to be taking quite a lot of effort to break the procedure and make such concatenated string call from Java as below. Want to understand from experts , if there is any other easy way exists in which we can just call the whole .sql file/anonymous block from Java?

    In one of the blog i saw below sample code to call the plsql block from Java but it seems will consume quite a ot of time to convert each of the plsql block to this kind of concatenated string.

    String plsql = "" +
    " declare " +
    " p_id varchar2(20) := null; " +
    " l_rc sys_refcursor;" +
    " begin " +
    " p_id := ?; " +
    " ? := 'input parameter was = ' || p_id;" +
    " open l_rc for " +
    " select 1 id, 'hello' name from dual " +
    " union " +
    " select 2, 'peter' from dual; " +
    " ? := l_rc;" +
    " end;";

    CallableStatement cs = c.prepareCall(plsql);


    **************Existing Plsql procedure Sample:-

    create or replace procedure p1(IP_ARG1, IP_ARG2, OP_ARG3,OP_ARG4)
    Cursor mycur(IP_ARG1) is ....
    select..
    from tab1 where table_name=IP_ARG1;

    begin

    For myrec in mycur(IP_ARG1)
    Loop
    ...
    END Loop;
    Exception
    .............
    END;


    ************ Converted Sample Plsql Block to be called from java****************

    DECLARE

    IP_ARG1 VARCHAR2(4000);
    IP_ARG2 VARCHAR2(4000);
    .......

    Cursor mycur(IP_ARG1) is ....
    select..
    from tab1 where table_name=IP_ARG1;

    begin

    For myrec in mycur(IP_ARG1)
    Loop
    ...
    END Loop;

    end;
    /

  2. #2
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Calling anonymous plsql blocks

    This article gives you the information you need to begin working with strings in your PL/SQL programs.

Similar Threads

  1. PLSQL Parser required for Unicode
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: December 13th, 2013, 12:12 AM
  2. [SOLVED] Using Anonymous Classes
    By EOSD598 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 28th, 2013, 09:52 PM
  3. Help with Anonymous Inner class
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 4th, 2013, 01:01 AM
  4. Anonymous class Example : What's wrong with my code?? :(
    By JavaEnthusiast in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 03:25 PM
  5. Anonymous is here
    By anonymous in forum Member Introductions
    Replies: 2
    Last Post: March 1st, 2010, 03:10 AM