Execute anonymous PL/SQL block via JDBC - OUT parameter not available

I have a simple proc on the database:
CREATE PROCEDURE TEST(X OUT BINARY_INTEGER, Y IN VARCHAR) AS
BEGIN
  X := 33;
END; I am trying to invoke it via JDBC using an anonymous PL/SQL block:
        try {
            Connection connection =
              DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
                "scott", "tiger");
            CallableStatement stproc_stmt = connection.prepareCall(
                "DECLARE\n" +
                " X_TARGET BINARY_INTEGER;\n" +
                " Y_TARGET VARCHAR(20) := :2;\n" +
                "BEGIN\n" +
                " TEST(X=>X_TARGET, Y=>Y_TARGET);\n" +
                " :1 := X_TARGET;\n" +
                "END;"
            stproc_stmt.registerOutParameter(1, Types.NUMERIC);
            stproc_stmt.setString(2, "test");
            stproc_stmt.executeUpdate();
            Object o = stproc_stmt.getObject(1);
        catch (Exception e) {
            e.printStackTrace();
        } No exceptions are thrown, but the Object o does not get the value '33' - it is NULL.
Any ideas?
thanks in advance,
Mike Norman

I think the issue may be in how JDBC parameter binding is being managed throughout the block's lifecycle.
The slightly-different TEST2 works:
CREATE PROCEDURE TEST2(Y IN VARCHAR, X OUT BINARY_INTEGER) AS
BEGIN
  X := 33;
END;
try {
    Connection connection =
      DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
     "scott", "tiger");
    CallableStatement stproc_stmt = connection.prepareCall(
     "DECLARE\n" +
     " Y_TARGET VARCHAR(20) := :1;\n" +
     " X_TARGET BINARY_INTEGER;\n" +
     "BEGIN\n" +
     " TEST2(Y=>Y_TARGET, X=>X_TARGET);\n" +
     " :2 := X_TARGET;\n" +
     "END;"
    stproc_stmt.setString(1, "test");
    stproc_stmt.registerOutParameter(2, Types.NUMERIC);
    stproc_stmt.executeUpdate();
    Object o = stproc_stmt.getObject(1);
catch (Exception e) {
    e.printStackTrace();
}The order of the bind indices ':1' and ':2' are reversed in the above anonymous block - we are returning via ':2'.
I am wondering if 'under the covers' there isn't perhaps a cursor issue. When the original block is parsed and the first bind index is found to be position 2, somehow we can't go back to position 1 - a forwards-only cursor?

Similar Messages

  • How to re-execute anonymous PL/SQL block in package definition ?

    Hi all,
    I implemented a package which contains procedures and an anonymous PL/SQL block.
    This anonymous PL/SQL block is executed only once when the package is called.
    and charge in-memory the content of table to avoid multiple SQL access each
    time one procedure is called.
    As my application open many sessions to the Oracle database, I would like to try
    a solution to signal all sessions to reload the content of table when the content
    of table is modified. The solution to stop and to restart the connection is not
    acceptable.
    Best regards
    Sylvain

    > .. to avoid multiple SQL access each time one procedure is called.
    As I understand your posting, this is the actual technical requirement. You want to force serialisation of PL/SQL code. Correct? (only one session at a time can run the procedure)
    This feature typically used to accomplish this in o/s code is called a semaphore. PL/SQL does not specifically support semaphores. However, it supports a range of IPC (Inter Process Communication) methods - from message queues to pipes.
    One of these IPC interfaces is DBMS_LOCK - which allows a unique lock to be defined and processes to manage their resource usage/execution/etc via this lock using the DBMS_LOCK API.
    I've found this a pretty clean and manageable solution to enforce serialisation. Of course, it is even better not to enforce serialisation. Rather design the code to be thread safe and capable of multi-processing/parallel processing.
    Personally, I would not use a table as an IPC mechanism as Oracle already provides better IPC mechanisms for PL/SQL code. As for "signalling sessions to re-load the table" - not possible as Oracle sessions cannot register callbacks to handle events. Oracle sessions are not event driven processes from a PL/SQL (application development) perspective.

  • JDBC: send batch of SQL commands as anonymous PL/SQL block

    Hi All,
    I did a little measurement to see if I can improve jdbc
    applications by batching dissimilar SQL commands into one
    anonymous PL/SQL block and execute it once. To my surprise, for
    a batch of 5 SQL commands, it take 60% more time than execute
    each of the 5 SQL commands separately.
    The same JDBC code, using similar SQL text batching, running
    against Sybase or MSSQL shows 60%-300% improvement.
    Is there any other way to batch dynamic dissimilar SQL commands
    in Oracle other than using anonymous PL/SQL block? Here is an
    example of how the "text-batching" PL/SQL block looks like:
    "begin insert into testtab1(col1, col2) values(1, 'row1');
    insert into testtab2(col1, col2) values(100, 1);....; end;"
    Thanks,
    Nam Nguyen
    null

    If you do:
    declare
    l_sql varchar2(32767);
    l_value varchar2(32767);
    begin
    select query_sql into l_sql from lov where lov_id = 100;
    dbms_output.put_line(l_sql);
    end;
    You'll see something like that:
    SELECT
    l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description, '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
    FROM ACTIVITY a
    ,MOUNTAINEERING m
    ,LOV l
    ,LOV l2
    WHERE a.JSATFA_ID = 82
    AND a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
    AND m.ACTIVITY_ID(+) = a.ACTIVITY_ID
    AND m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
    you need to duplicate the '
    you can do many things like:
    CTH@> select * from sqls;
    C
    select first_name || ' ' || last_name as value1 from employees where rownum=1
    1 fila seleccionada.
    CTH@>
    CTH@> ;
    1 declare
    2 l_sql varchar2(32767);
    3 l_value varchar2(32767);
    4 type generic_cursor is ref cursor;
    5
    6 c generic_cursor;
    7
    8 begin
    9 select replace(c, ''', ''''') into l_sql from sqls;
    10
    11 execute immediate l_sql into l_value;
    12 dbms_output.put_line(l_value);
    13* end;
    CTH@> /
    Ellen Abel
    Procedimiento PL/SQL terminado correctamente.
    CTH@>

  • Alter database statement in anonymous pl/sql block

    Is it possible to include an alter database statement in an anonymous pl/sql block?
    When I execute this code to query user_tables for all table names, disable their constraints and drop the table, I got the following error:
    ***MY CODE
    -- DECLARE VARIABLE(S)
    DECLARE
         v_TABLE_NAME TABLE_NAME.USER_TABLE%TYPE;
    -- DECLARE AND DEFINE CURSOR
    CURSOR c_GETTABLES is
         SELECT TABLE_NAME from USER_TABLES;
    BEGIN
    OPEN c_GETTABLES;
    LOOP
    FETCH c_GETTABLES into v_TABLE_NAME;
    EXIT when c_GETTABLES%notfound;     
    ALTER TABLE v_TABLE_NAME DISABLE PRIMARY KEY CASCADE;
    DROP TABLE v_TABLE_NAME;
    END LOOP;
    CLOSE c_GETTABLES;
    END;
    ***RESPONSE FROM SERVER
    ALTER TABLE v_TABLE_NAME DISABLE PRIMARY KEY CASCADE;
    ERROR at line 15:
    ORA-06550: line 15, column 1:
    PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
    begin case declare exit for goto if loop mod null pragma
    raise return select update while with <an identifier>
    <a double-quoted delimited-identifier> <a bind variable> <<
    close current delete fetch lock insert open rollback
    savepoint set sql execute commit forall merge
    <a single-quoted SQL string> pipe
    Thanks

    When you want to perform ddl statements in a (anonymous) PL/SQL block, you have to use dynamic SQL because ddl is not possible in pl/sql.
    Dynamic sql means that you sort of execute ddl statements in a sql manner. To use dynamic sql, two options exist:
    - dbms_sql package : for oracle before 8i. To use this package is not always easy. Read about it carefully first before using.
    - Native Dynamic SQL : implemented in 8i and very easy to use. An example would be :
    declare
    lv_statement varchar2(32676);
    begin
    lv_statement := 'ALTER TABLE MY_TABLE DISABLE CONSTRAINT MY_TABLE_CK1';
    execute immediate lv_statement;
    lv_statement := 'ALTER TABLE MY_TABLE ENABLE CONSTRAINT MY_TABLE_CK1';
    execute immediate lv_statement;
    end;
    Good luck.
    Edwin van Hattem

  • Anonymous PL/SQL block within a select statement

    I read somewhere that it was possible to build an anonymous pl/sql block within a sql statement. Something along the lines of:
    select
    begin
    i = 0;
    return i;
    end;
    from dual;
    However, for the life of me, I can't find documentation on this. Could someone please point me to the right place, assuming I'm not just imagining it.
    Thanks.

    Did you mean, executing a pl/sql block generated from an sql.
    declare
    cmd varchar2(100);
    begin
    select 'declare i pls_integer := 0; begin i:=1; end;' into cmd from dual;
    execute immediate cmd;
    end;
    [pre]                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Pass values to Guid collection/array parameter for anonymous pl/sql block

    The following code pops the System.ArgumentException: Invalid parameter binding
    Parameter name: p_userguids
    at Oracle.DataAccess.Client.OracleParameter.GetBindingSize_Raw(Int32 idx)
    at Oracle.DataAccess.Client.OracleParameter.PreBind_Raw()
    at Oracle.DataAccess.Client.OracleParameter.PreBind(OracleConnection conn, IntPtr errCtx, Int32 arraySize)
    at Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery()
    Any help or advice ?
    anonymous pl/sql block text:
    DECLARE
    TYPE t_guidtable IS TABLE OF RAW(16);
    p_userguids t_guidtable;
    BEGIN
    DELETE testTable where groupname=:groupname;
    INSERT INTO testTable (userguid, groupname)
    SELECT column_value, :groupname FROM TABLE(p_userguids);
    END;
    c# code:
    public static void SetGroupUsers(string group, List<Guid> users)
    OracleConnection conn = Database.ConnectionEssentus;
    try
    conn.Open();
    OracleCommand sqlCmd = new OracleCommand();
    sqlCmd.CommandText = sqls["SetGroupUsers"]; // above anonymous block
    sqlCmd.Connection = conn;
    sqlCmd.BindByName = true;
    OracleParameter p_guidCollection = sqlCmd.Parameters.Add("p_userguids", OracleDbType.Raw);
    p_guidCollection.Size = users.Count;
    p_guidCollection.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
    p_guidCollection.UdtTypeName = "t_guidtable";
    p_guidCollection.Value = users.ToArray();
    sqlCmd.Parameters.Add("groupname", OracleDbType.Varchar2, 30).Value = group;
    sqlCmd.ExecuteNonQuery();
    catch(Exception ex)
    System.Diagnostics.Debug.WriteLine(ex.ToString());
    finally
    conn.Close();
    }

    New question,
    How can I select records using "in" condition clause likes the following sentence?
    SELECT userguid, firstname, lastname FROM UserTable WHERE userguid in (SELECT column_value FROM TABLE(p_userguids))
    I tried using PIPE ROW like this, but ORACLE said "PLS-00629: PIPE statement cannot be used in non-pipelined functions"
    FOR i in p_userguids.first .. p_userguids.last
    LOOP
    SELECT userguid, firstname, lastname INTO l_userrecord FROM UserTable WHERE userguid=p_userguids(i);
    PIPE ROW(l_userrecord);
    END LOOP;

  • ODP 10.2.0.100 + anonymous PL/SQL block

    hi,
    my ODP dont wants anonymous PL/SQL blocks...
    my code:
    OracleConnection conn = new OracleConnection(sConnectionString);
    string text =
    "declare
    d int;
    begin
    SELECT nr INTO d FROM TMPTEXTS;
    end;
    OracleCommand cmd = new OracleCommand(text, conn);
    cmd.CommandType = CommandType.Text;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    // error message
    Oracle.DataAccess.Client.OracleException ORA-06550: Zeile 1, Spalte 1:
    PLS-00103: encountered the symbol "" expecting
    begin case declare exit for function ...
    any ideas ?
    lg dan

    The sample code at
    http://www.oracle.com/technology/oramag/oracle/06-jan/o16odpnet.html
    demonstrates using an anon block with a bind.

  • Create publicsynonyms for anonymous pl/sql block

    Hi,
    How do I create a public synonym for an anonymous PL/SQL block?
    BEGIN
         IF a IS NOT NULL
         THEN
              OPEN b;
              LOOP
                   FETCH b INTO Func;
              EXIT WHEN b%NOTFOUND;
                   c := c || Func || ',';
              END LOOP;
              CLOSE b;
              c := SUBSTR(c, 1, LENGTH(TRIM(c))-1);
         END IF;
         return c;
    END abc;
    ERROR at line 2:
    ORA-06550: line 2, column 5:
    PLS-00201: identifier 'a' must be declared
    ORA-06550: line 2, column 2:
    PL/SQL: Statement ignored
    ORA-06550: line 19, column 2:
    PLS-00372: In a procedure, RETURN statement cannot contain an expression
    ORA-06550: line 19, column 2:
    PL/SQL: Statement ignored

    Hi,
    CrackerJack wrote:
    Hi,
    How do I create a public synonym for an anonymous PL/SQL block?A synonym is an alternate name.
    Anonymous bblocks, by definition, don't have names, so they can't have alternate names. Also, they're not stored, so what good would it do if you could have synonyms for them?
    An anonymous block is just a quick and dirty alternative to a procedure, anyway. Why not make a procedure, or a function?
    BEGIN
         IF a IS NOT NULL
         THEN
              OPEN b;
              LOOP
                   FETCH b INTO Func;
              EXIT WHEN b%NOTFOUND;
                   c := c || Func || ',';
              END LOOP;
              CLOSE b;
              c := SUBSTR(c, 1, LENGTH(TRIM(c))-1);
         END IF;
         return c;
    END abc;
    ERROR at line 2:
    ORA-06550: line 2, column 5:
    PLS-00201: identifier 'a' must be declared
    ORA-06550: line 2, column 2:
    PL/SQL: Statement ignored
    ORA-06550: line 19, column 2:
    PLS-00372: In a procedure, RETURN statement cannot contain an expression
    ORA-06550: line 19, column 2:
    PL/SQL: Statement ignoredThis code looks like part of a function, that was named abc. Where is the beginning part of that function? The errors are caused because it is missing the part where this was declared as a function, and the part where the local variables a, c and func, and the cursor b, were defined.
    Does thsi function have something to do with the original question: "How do I create a public synonym for an anonymous PL/SQL block?"?
    What are you trying to do?

  • Debugging Anonymous PL/SQL Block

    Does sql developer allow you to debug an anonymous PL/SQL block or is the debugging restricted to compiled procedures/functions? Am a TOAD user but am looking at SQL Developer. Thanks

    As you can only set breakpoints inside a code editor, you'll have to wrap your code in a stored procedure.
    You could request this at the SQL Developer Exchange, but I guess it'll be too much work to add vs. the relative ease of using a stored proc.
    Regards,
    K.

  • Running a anonymous PL/SQL block

    Hi,
    I have created an anonymous PL/SQL block and saved it in a file. And I am now trying to run it from the sql prompt using @.
    But this doesn't seem to be working. I get a wierd number as output and then it hangs.
    This is not the case if I copy paste the anonymous block. In this case I get the correct output.
    Please assist as to whether it is possible to run it using @ as it is important for me to do so..

    Hi,
    You forgot the slash after the anonymous block:
    USER is "YJAM"
    TEST>-- Create anonymous Block
    TEST>BEGIN
      2    NULL;
      3  END;
    4 /
    PL/SQL procedure successfully completed.
    TEST>-- save as script
    TEST>save ab.sql
    Created file ab.sql
    TEST>get ab
      1  BEGIN
      2    NULL;
      3* END;
    TEST>@ab
    PL/SQL procedure successfully completed.
    TEST>ed ab Here, I delete line 4. hence the Block won't run.
    TEST>@ab
      4
      5
      6
      7
      8  . a dot to exit input mode. a slash would run the block
    TEST>Regards,
    Yoann.

  • Anonymous PL SQL block runs in 2 minutes but runs for 4 hours in Applicatio

    We are facing an issue with a custom code. When we run the custom code as anonymous pl/sql block , it completes in 2 minutes.
    But when we run the same from Oracle Application as a concurrent program, it runs for more than 4 hours.
    There is absolute no change in the code.
    Anyone faced this issue?

    Does your code use context sensitive views such as po_headers?
    Maybe you have not set the context in the pl/sql block so the view returns 0 records.
    But when you run it in Apps, the context is automatically set and so it processes a large number of records.
    Set the context if you have not and then try again.
    begin
    dbms_application_info.set_client_info('&org_id'); --
    end;
    Sandeep Gandhi

  • Executing a PL/SQL block (using Toplink)

    I have a scenario where I need to execute some fairly complex PL/SQL blocks. As a tester, I am attempting to execute the following simple block:
    declare val NUMBER := 1; begin val := 2; end;
    Both wrapping this in an SQLCall, or a DataReadQuery give the following exception. What is the best way to execute a PL/SQL block using Toplink?
    Local Exception Stack:
    Exception [TOPLINK-4002] (Oracle TopLink - 10g Release 3 (10.1.3.0.0) (Build 060118)): oracle.toplink.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: ORA-00900: invalid SQL statement
    Error Code: 900
    Call: declare val NUMBER := 1; begin val := 2; end;
    Query:DataReadQuery()
         at oracle.toplink.exceptions.DatabaseException.sqlException(DatabaseException.java:290)
         at oracle.toplink.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:570)
         at oracle.toplink.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:442)
         at oracle.toplink.threetier.ServerSession.executeCall(ServerSession.java:453)
         at oracle.toplink.internal.queryframework.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:117)
         at oracle.toplink.internal.queryframework.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:103)
         at oracle.toplink.internal.queryframework.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:174)
         at oracle.toplink.internal.queryframework.DatasourceCallQueryMechanism.executeSelect(DatasourceCallQueryMechanism.java:156)
         at oracle.toplink.queryframework.DataReadQuery.executeNonCursor(DataReadQuery.java:118)
         at oracle.toplink.queryframework.DataReadQuery.executeDatabaseQuery(DataReadQuery.java:110)
         at oracle.toplink.queryframework.DatabaseQuery.execute(DatabaseQuery.java:603)
         at oracle.toplink.queryframework.DataReadQuery.execute(DataReadQuery.java:96)
         at oracle.toplink.publicinterface.Session.internalExecuteQuery(Session.java:2062)
         at oracle.toplink.publicinterface.Session.executeQuery(Session.java:981)
         at oracle.toplink.publicinterface.Session.executeQuery(Session.java:938)

    Could you try the following:
            Session s = ...
            DataModifyQuery dmq = new DataModifyQuery();
            SQLCall sqlCall = new SQLCall();
            sqlCall.setQueryString(
                "declare\n" +
                "  val NUMBER := 1;\n" +
                "begin\n" +
                "  val := 2;\n" +
                "end;");
            sqlCall.setQuery(dmq);
            dmq.setCall(sqlCall);
            s.executeQuery(dmq);

  • I have a MacBook Pro (Retina) and a Sony HandyCam DCR-HC48 video Camera. The Sony only exports video via Firewire, which is not available on the Mac. Is there a firewire to USB or to HDMI adapter?

    I have a MacBook Pro (Retina) and a Sony HandyCam DCR-HC48 video Camera. The Sony only exports video via Firewire, which is not available on the Mac. Is there a firewire to USB or to HDMI adapter?

    tyler57,
    it would be better to direct that question to Gary Loftis, since my MacBook Pro doesn’t have a Thunderbolt port.

  • My new filters tree flames and picture frame are greyed out and not available?

    my new filters tree flames and picture frame are greyed out and not available. what do I need to do?

    It's likely that your graphics card/GPU does not support the new feature. If you are using an older video card or an integrated graphics card that is not new like the Intel Iris or Iris pro than that is likely the reason.

  • Why named parameter can't be used multiple times in PL/SQL block in JDBC

    with the following PL/SQL block, when I run int in JDBC, I get an error,
    it says, The number of parameter names does not match the number of registered parameters.
    if all named parameters are used only once, then my program works fine.
    My old program uses Oracle Forms to run the attached PL/SQL block correctly, I just want to run them in JDBC without more efforts, I don't want to rewrite all PL/SQL blocks.
    Does oracle driver support this case? why the PL/SQL block can work in Oracle Forms but failed in JDBC?
    Can we have an another solutions to avoid rewriting the PL/SQL block to stored procedure?
    if I use following SQL:
    BEGIN if :q is null then :q := 'X'; else :q := 'Y'; end if; END;
    , Using java program:
    import java.sql.*; public class RunPLSQLBlock { public static void main(String s[]) throws SQLException { String URL = "jdbc:oracle:thin:@192.168.11.199:1521:TIBSTEST"; Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = (Connection) DriverManager.getConnection(URL, "FBP1DEV", "FBP1DEV"); String SQL = "BEGIN  if :q is null then  :q := 'X'; else :q := 'Y'; end if; END;"; CallableStatement stmt = con.prepareCall(SQL); stmt.registerOutParameter("q", Types.VARCHAR); stmt.setString("q", "A"); stmt.execute(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.close(); } } } }
    in the coding, only "q" registered, I got:
    java.sql.SQLException: The number of parameter names does not match the number of registered praremeters at oracle.jdbc.driver.OracleSql.setNamedParameters(OracleSql.java:314) at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:10096) at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:5693) at RunPLSQLBlock.main(RunPLSQLBlock.java:28)
    now, tried to register 3 indexes, changed fragments are below.
    import java.sql.*; public class RunPLSQLBlock { public static void main(String s[]) throws SQLException { String URL = "jdbc:oracle:thin:@192.168.11.199:1521:TIBSTEST"; Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = (Connection) DriverManager.getConnection(URL, "FBP1DEV", "FBP1DEV"); String SQL = "BEGIN  if :q is null then  :q := 'X'; else :q := 'Y'; end if; END;"; CallableStatement stmt = con.prepareCall(SQL); stmt.registerOutParameter(1, Types.VARCHAR); stmt.registerOutParameter(2, Types.VARCHAR); stmt.registerOutParameter(3, Types.VARCHAR); stmt.setString(1, "A"); stmt.execute(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.close(); } } } }
    now error changed to:
    java.sql.SQLException: ORA-01006: bind variable does not exist at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:457) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:400) at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:926) at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:476) at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:200) at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:543) at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:208) at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1416) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1757) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4372) at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:4595) at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:10100) at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:5693) at RunPLSQLBlock.main(RunPLSQLBlock.java:26)
    , now tried register only 1 position like below,
      CallableStatement stmt = con.prepareCall(SQL);   stmt.registerOutParameter(1, Types.VARCHAR);   stmt.setString(1, "A");   stmt.execute();
    , it says:
    java.sql.SQLException: Missing IN or OUT parameter at index:: 2 at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:2177) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4356) at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:4595) at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:10100) at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:5693) at RunPLSQLBlock.main(RunPLSQLBlock.java:26)
    , now let try a OK case, which use all named parameters only once. coding like below, SQL and Java listed below.
    BEGIN if :q is null then :r := 'X'; else :s := 'Y'; end if; EXCEPTION   WHEN NO_DATA_FOUND THEN     NULL; END;
    import java.sql.*; public class RunPLSQLBlock { public static void main(String s[]) throws SQLException { String URL = "jdbc:oracle:thin:@192.168.11.199:1521:TIBSTEST"; Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = (Connection) DriverManager.getConnection(URL, "FBP1DEV", "FBP1DEV"); String SQL = "BEGIN  if :q is null then  :r := 'X'; else :s := 'Y'; end if; END;"; CallableStatement stmt = con.prepareCall(SQL); stmt.registerOutParameter("q", Types.VARCHAR); stmt.registerOutParameter("r", Types.VARCHAR); stmt.registerOutParameter("s", Types.VARCHAR); stmt.setString("q", "A"); stmt.execute(); System.out.println("Q :" + stmt.getString("q")); System.out.println("R :" + stmt.getString("r")); System.out.println("S :" + stmt.getString("s")); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.close(); } } } }
    , the case give us the following output:
    Q :A R :null S :Y
    2nd part, I also tried another scheme, to use 'execute immediate', test code attached below, it also have errors.
    begin execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;' using in out :q, out :r; end;
    , Java Code:
    import java.sql.*; public class RunDynamicSQL { public static void main(String s[]) throws SQLException { String URL = "jdbc:oracle:thin:@192.168.11.199:1521:TIBSTEST"; Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = (Connection) DriverManager.getConnection(URL, "FBP1DEV", "FBP1DEV"); String SQL ="begin execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;' using in out :q, out :r; end;"; CallableStatement stmt = con.prepareCall(SQL); stmt.registerOutParameter("q", Types.VARCHAR); stmt.registerOutParameter("r", Types.VARCHAR); stmt.setString("q", "A"); stmt.execute(); System.out.println("Q :" + stmt.getString("q")); System.out.println("R :" + stmt.getString("r")); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.close(); } } } }
    , the output is, we can find when parameter 'q' is IN OUT mode, we can't get its final value:
    Q :null R :Z
    , now I tried my workaround, it works fine by using a temporary variable, now my named parameter is split to 2 roles, one is for IN, another is for OUT, now I can get final out value.
    declare q clob; r clob; begin q := ?; r := ?; execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;' using in out q, out r; ? := q; ? := r; end;
    , my test java code,
    import java.sql.*; public class RunDynamicSQL { public static void main(String s[]) throws SQLException { String URL = "jdbc:oracle:thin:@192.168.11.199:1521:TIBSTEST"; Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = (Connection) DriverManager.getConnection(URL, "FBP1DEV", "FBP1DEV"); String SQL ="declare q clob;r clob; begin q := ?; r := ?; execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;' using in out q, out r; ? := q; ? := r; end;"; CallableStatement stmt = con.prepareCall(SQL); stmt.registerOutParameter(3, Types.VARCHAR); stmt.registerOutParameter(4, Types.VARCHAR); stmt.setString(1, "A"); stmt.setString(2, "A"); stmt.execute(); System.out.println("Q :" + stmt.getString(3)); System.out.println("R :" + stmt.getString(4)); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.close(); } } } }
    , the output is expected,
    Q :Y R :Z
    Database:
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    JDBC Driver, extracted from ojdbc6_g.jar/META-INF/MANIFEST.MF :
    Created-By: 1.5.0_30-b03 (Sun Microsystems Inc.)
    Implementation-Vendor: Oracle Corporation
    Implementation-Title: JDBC debug
    Implementation-Version: 11.2.0.3.0
    Repository-Id: JAVAVM_11.2.0.3.0_LINUX_110823
    Specification-Vendor: Sun Microsystems Inc.
    Specification-Title: JDBC
    Specification-Version: 4.0
    Main-Class: oracle.jdbc.OracleDriver
    JDK:
    java version "1.7.0"
    Java(TM) SE Runtime Environment (build 1.7.0-b147)
    Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)
    Edited by: jamxval on 2013-3-22 2:01PM (UTC+08:00), Give full test java program and SQL, added environment/API level; Attached another problem.
    Edited by: jamxval on 2013-3-26 17:57 (UTC +08), Adjust code style

    Hi, thanks for your response, now I see, the named parameter is for stored procedure only, for PL/SQL block we name it placeholder name.
    After cast my java.sql.CallableStatement to oracle.jdbc.OracleCallableStatement, I can find setStringAtName,
    now, I have only one question:I can't find corresponding methods for registerOutputParameter, how we fetch output value?
    I tried to callableStatement.getString("q"); it reports errors, but there are no ordinal binding in my source code, does placeholder names doesn't support OUT mode?
    Java:
    CallableStatement stmt = con.prepareCall("BEGIN  if :q is null then  :r := 'X'; else :s := 'Y'; end if; END;");
    oracle.jdbc.OracleCallableStatement call = (oracle.jdbc.OracleCallableStatement) stmt;
    call.registerOutParameter("q", Types.VARCHAR);
    call.registerOutParameter("r", Types.VARCHAR);
    call.registerOutParameter("s", Types.VARCHAR);
    call.setStringAtName("q", "A");
    call.setStringAtName("r", "A");
    call.setStringAtName("s", "A");
    call.execute();
    System.out.println("Q :" + call.getString("q"));
    </Java>
    <output>
    java.sql.SQLException: 不允许的操作: Ordinal binding and Named binding cannot be combined!
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
         at oracle.jdbc.driver.OracleCallableStatement.getString(OracleCallableStatement.java:2834)
         at RunPLSQLBlock.main(RunPLSQLBlock.java:33)
    </output>by the way, in my below-mentioned SQL 'problematic', when my code uses 'execute immediate' and use placeholder names in IN OUT mode, we always get NULL value (i.e. ':q'), but we can get final value of ':r' when ':r' is OUT mode only; now I get a workaround attached in below-mentioned 'my workaround' block, which split the IN OUT roles to 2 parts, it can work now;
    It seems that the difference between 'problematic' and 'my workaround' imply that there are something work unexpectedly when the driver process the placeholder names, because 'my workaround' and ':r in problematic case' make sure the 'execute immediate' returned output values correctly, unluckly driver layer can't get return values.
    <SQL name = 'problematic'>
    begin
         execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;'
         using in out :q, out :r;
    end;
    </SQL>
    <SQL name='my workaround'>
    declare     
         q clob;
         r clob;
    begin
         q := ?;
         r := ?;
         execute immediate 'begin if :q is null then :q := ''X''; else :q := ''Y''; :r := ''Z''; end if; end;' using in out q, out r;
         ? := q;
         ? := r;
    end;Edited by: EJP on 26/03/2013 14:14

Maybe you are looking for

  • Template to create a 3D box set of books

    I'm looking for a template to create a 3D boxed set of books with the cover on the right side and three spines to the left. There was a really nice tutorial on how to do it on YouTube and the woman had the templates available for 99 cents on Amazon K

  • HT4009 Problem with in apps purchase. Pls help

    Since about a week now I can no longer purchase gems in fashion story or my bakery.     I do not have a restriction set up in my iPad.     Previous billing went through okay I think.  I get message. Contact iTunes support to complete this transaction

  • IOS 4.1 on an iPhone 3Gs

    I have upgraded to iOS 4.1 on my iPhone 3GS and now my alarm clock does not match the time on the phone! It goes off 1 hour before the intended time! I have it on auto time and have resubmitted my alarms, also know of five people who this is happenin

  • Workflow approval - 'n' step BADI, Approval hierarchy problem

    Hi, Description of the issue: For a shopping cart approval, the approver's list determined by the 'n' step approval BADI does not include the user who is the immediate in the org. hierarchy. Rather the work item goes to the superior authority (say MD

  • Query executing faster after restart

    Hi, I have a query which used to take around 60 seconds to execute. The query is mainly made up of joins and has an order by condition. The query was showing a service time of 125ms when accessing the temp datafile. When I restarted the server (windo