Debugging PL/SQL blocks in SQL Developer

Is there any way to debug blocks of PL/SQL in SQL Developer by setting breakpoints in the code? I'm using SQL Developer.
I'm trying to debug some fairly complex PL/SQL blocks - they are not procedures or functions and so I'm unable to run them in debug mode.
p.s. Apologies should have posted this in the database forum really.
Edited by: Antilles on Sep 10, 2008 1:12 AM

Hello,
Sure you can, take a look at the following two links -
http://sueharper.blogspot.com/2006/07/remote-debugging-with-sql-developer_13.html
http://www.oracle.com/technology/oramag/oracle/08-may/o38browser.html
Hope this helps,
John.
http://jes.blogs.shellprompt.net
http://www.apex-evangelists.com

Similar Messages

  • Pl/sql block returning sql query.

    Hello,
    I am using oracle 10g apex 3.2 version.
    I am using the following return statement inside my report which is pl/sql block returning sql query.
    declare
    pid varchar2(100);
    begin
    return 'select patient_id_code from t_files_data_exp where patient_id_code not in pid';
    end;
    How am i suppose to mention the pid inside the return stmt i mean with any quotes or anything? because the above return stmt gives error
    "1 error has occurred
    Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the ''generic columns'' checkbox below the region source to proceed without parsing. The query can not be parsed, the cursor is not yet open or a function returning a SQL query returned without a value."
    Thanks

    Hello,
    I did exactly the way u told
    declare
    pid varchar2(100) := '(61092,61093)';
    begin
    return 'select patient_id_code from t_files_data_exp where patient_id_code not in ' || pid;
    end;
    patient_id_code is varchar2(100) only in table.
    For this i am getting "invalid number error".
    Thanks

  • Using item values in a pl/sql block or sql query in a process

    Hi,
    I am new to apex. Developing my first application.
    I have created a form with 2 items. Based on the values in these items, when I click a button, want to execute a
    query.
    eg: emp_name(item1), deptno(item2). When I clock on find button, I want to fetch the record and disply the resultant
    columns in other created items like sal,comm etc.
    How can I do this?
    I tried the following pl/sql block in a process
    begin
    select sal,comm into p1_sal,p1_comm from emp where name=p1_name and deptno=deptno;
    end;
    But it is not accepting the page items in the block.
    How to achieve this?
    Thanks,
    Kavitha

    We have many OBEs, tutorials, etc. Please visit the <a hef="http://www.oracle.com/technetwork/developer-tools/apex/learnmore/index.html" target="_window">Learn More</a> tab of our otn site.
    This is the section of the User's Guide that discusses session state -
    -- Sharon

  • PL/SQL block to sql case

    Hello,
    I have a newbie sql question:
    I have the following pl/sql block that I want to transform in sql case code:
                 IF Num_WholesaleNonDiscountValue > 0.0 THEN
                      UsageStatus := 'CHARGE';
                      if Num_WholesaleNonDiscountValue < 1.0 then
                          WholesaleNonDiscountValue := '0' || TO_CHAR(Num_WholesaleNonDiscountValue);
                        else
                          WholesaleNonDiscountValue := TO_CHAR(Num_WholesaleNonDiscountValue);
                      end if;
                  ELSE
                      UsageStatus := 'FREE';
                      WholesaleNonDiscountValue := TO_CHAR(Num_WholesaleNonDiscountValue);
                  END IF;All I do is the following code:
    select (case when Num_WholesaleNonDiscountValue > 0.0 then
                              (case Num_WholesaleNonDiscountValue < 1.0 then  '0' || TO_CHAR(Num_WholesaleNonDiscountValue
                               else TO_CHAR(Num_WholesaleNonDiscountValue))
             else TO_CHAR(Num_WholesaleNonDiscountValue)) from table_name;I do not know how to evaluate the UsageStatus variable, where to put it in the select statement.
    This is for my learning process.
    Thanks in advanced,
    Dan
    Edited by: danut68 on Jan 13, 2010 5:52 AM

    Hi, Dan,
    I think you want something like this:
    SELECT     CASE
              WHEN  Num_WholesaleNonDiscountValue > 0
              THEN  'CHARGE'
              ELSE  'FREE'
         END                              AS UsageStatus
    ,     CASE
              WHEN  Num_WholesaleNonDiscountValue > 0
              AND   Num_WholesaleNonDiscountValue < 1
              THEN  '0'
         END || TO_CHAR (Num_WholesaleNonDiscountValue)     AS WholesaleNonDiscountValue
    FROM     table_x
    ;A CASE expression returns one value.
    In PL/SQL, you can do two or more completely separate things (set two different variables, for example) in the same IF-THEN-ELSE block.
    To get the same results in SQL requires a completely different approach.
    The clearest is to use a different CASE expression for each of the things you're trying to do, as I did above. The WHEN clauses of those CASE statements may be very similar, even identical.
    Sometimes you can factor out some of the logic (using a sub-query, perhaps) so that the logic that has to be repeated in each CASE expression is very simple.
    Another option for setting two variables is to have a single CASE expression that returns a string wihich is the concatenation of two different varibales, which you later parse into two separate variables. I would do this only as a last resort.

  • How exit for a script having set of pl/sql blocks and sql queries

    HI,
    I have set of blocks and sql queries in a script.
    In some cases I want to stop the excution of next statements and blocks.
    As in pl/sql block We can use return , in case of loop we can use exit, so what is to be use in case if sql script which contain set of blocks and sql queries.
    Thanks and Regards in Advance,

    Hi,
    how to exit from the script if confirm_to_continue is set to 'N'.
    i.e in this case I want the preceding statements not to be excuted.
    Please suggest.
    script:
    declare /*BLOCK NO1*/
    begin
    IF &&confirm_to_continue = 'N'
    THEN
    ---exit from from whole script
    RETURN; -- this will only exit from this block
    END IF;
    end;
    host IF EXIST &file_name (del &file_name) ELSE (echo missing)
    declare /*BLOCK NO 2*/
    begin
    end;
    /

  • Oracle SQL Developer 3.0: PL/SQL debugging of anonymous blocks: ISSUES

    Hello,
    I just downloaded the Oracle SQL Developer 3.0. I have been using the EA releases as they came into existence and was happy to see the released version. So I immediately tried to debug an anonymous block (something I did not try to do in the EA releases), and nothing happened.
    The "Debug" was grayed out and the key-chord "ctrl-shift-F10" did nothing. I found this forum:
    Re: 30EA1: anonymous block debugging?
    and followed Vadim Tropashko's advice. This did nothing for my anon. block but worked fine for the simple example.
    So I started to whittle my anon. block down to find the culprit, here is a repeatable breaking point for me:
    declare
        stmt1 long;
        stmt2 long;
        stmt3 long;
        stmt4 long;
        p_data varchar2( 500 );
        i      varchar2( 10 );
    BEGIN
        STMT1 := 1;
        STMT2 := 1;
        STMT3 := 1;
        STMT4 := 1;
        --the moment this is in the block "Debug" is no longer an option
         select
            SendDocumentResult
           into
            p_data
           from
            XMLTABLE( '/data'
                    PASSING
                    xmltype.createxml( '<?xml version="1.0" encoding="utf-8"?><data><SendDocumentResult>test</SendDocumentResult></data>' )
                    COLUMNS SendDocumentResult varchar2( 1000 ) PATH 'SendDocumentResult' ) ;
    end;The moment I have the SELECT INTO ... XMLTABLE() it fails (a normal SELECT INTO works just fine).
    Is this a problem with my environment or is a problem with SQL Developer 3.0.04. Looking over K's comments, it seems the debug worked for 'simple' blocks, so I wonder if this is just out-of-scope...
    My environment:
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production    
    PL/SQL Release 11.2.0.1.0 - Production                                          
    CORE     11.2.0.1.0     Production                                                        
    TNS for IBM/AIX RISC System/6000: Version 11.2.0.1.0 - Production               
    NLSRTL Version 11.2.0.1.0 - Production
    and Oracle SQL Developer
    3.0.04 (Buld Main 04.34 with bundled Java) on a Window's XP box.

    Thanks, I'll survive. Just my luck, the first item I try to anon. debug didn't work! :)
    thanks, hopefully this problem will be few and far between

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

  • Can't create a sequence within a pl/sql block with execute immediate.

    Hi All. I created a user and granted it the 'create sequence' privilege though a role. In a pl/sql block I try to create a sequence using 'execute immediate' but get a 1031-insufficient privileges error. If I grant create sequence directly to the user, the pl/sql block completes successfully. Can anyone explain this behavior? We're running 11.2 Enterprise Editon.
    Thanks,
    Mark

    In a definer's rights stored procedure (the default), you only have access to privileges that have been granted directly, not via a role.
    There are two basic reasons for that. First, roles can be enabled or disabled, default and non-default, password-protected, etc. so the set of roles a particular user actually has is session-specific. Oracle needs to know at compile time what privileges the owner of the procedure has. The only way to do that (without deferring the privilege check) is to ignore privileges granted through roles.
    Second, since 99% of privilege management DBAs do involves granting and revoking roles, it's helpful that changing role privileges will never cause objects to be marked invalid and recompiled which can have side-effects on applications. DBAs only need to worry about causing problems on those rare cases where they are granting or revoking direct privileges to users.
    You can create an invoker's rights stored procedure by adding the clause (AUTHID CURRENT_USER). That defer's the security check to run-time but allows the procedure to see privileges granted through roles in the current session. But that means that the caller of the procedure would need to have the CREATE SEQUENCE privilege through the role, not the owner of the procedure.
    And just to make the point, dynamic object creation in PL/SQL is almost always a red flag that there is something problematic in your design. If you are creating sequences dynamically, that means that you'd have to refer to them dynamically throughout your code which means that your inserts would need to use dynamic SQL. That's not a particularly easy or safe way to develop code.
    Justin

  • PL/SQL block returning error

    Hi All,
    I have used PL/SQL block in my custom report to search table heading. Basically I have two select list (LOV), user select the parameter from the list and click on go, it displays the report fine when record exists.
    I am using following PL/SQL block -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_PARK_NAME IS NOT NULL THEN
    v_sql := 'select P.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, P.'|| :P126_PARK_NAME ||', C.COP_ID, L.OLIC_ID Olic_id '
    ||' from RTT_OP_PARKS_TEMP P, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_PARK_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND P.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=P.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Now, I want to use like condition to display all the records by checking the parameters in text field. I have created a new text field and modified the query as follows but it does not work -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_PARK_NAME IS NOT NULL THEN
    v_sql := 'select P.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, P.'|| :P126_PARK_NAME ||', C.COP_ID, L.OLIC_ID Olic_id '
    ||' from RTT_OP_PARKS_TEMP P, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_PARK_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND P.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=P.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    IF :P126_MARINE_PARK != -1 THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_MARINE_PARK ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_MARINE_PARK || ' IS NOT NULL '
    ||' M.'||:P126_MARINE_PARK||' IS LIKE :P126_MARINE_PARK||'%' '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Can anybody tell me what I am doing wrong here?
    Thanks in advance
    Regards,
    M Tajuddin

    Hi Andy,
    Thanks for pointing the quotation however it does not work either. Now I am getting -
    >
    failed to parse SQL query:
    ORA-00933: SQL command not properly ended
    >
    Now my query is like below -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_ZONE_NAME IS NOT NULL THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_ZONE_NAME ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_ZONE_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    IF :P126_MARINE_PARK IS NOT NULL THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_MARINE_PARK ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where ' || :P126_MARINE_PARK || ' IS NOT NULL '
    *||' M.' || :P126_MARINE_PARK || ' IS LIKE ''' || :P126_MARINE_PARK || '%'' '*
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Can you tell me how can I display dynamic multiple column in my query? Say in the LIKE condition if user type M in the parameter field and click on go, it must return more than one column starting column name M but in my current query it will only display one column, am I right?
    Thanks again for your suggestion.
    Kind regards,
    M Tajuddin
    http://tajuddin.whitepagesbd.com

  • 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

  • FETCHING OUTPUT  VALUE FROM PL/SQL BLOCK

    hI aLL,
    I'm working on ODP.NET. I'm executing below pl/sql command. while executing this command in pl/sql it's showing the output which is an xml.
    declare
    xmloutput xmltype;
    xmldata varchar(32767);
    teid t_id := t_en_id( 'L','L',NULL,12121,'ABC','USER','N');
    tgetinfo to_info := NULL;
    RETVAL NUMBER;
    begin
    RETVAL := GET_DTLS(teid,tgetinfo);
    dbms_output.put_line(RETVAL);
    xmldata := tgetinfo.XML_info.getclobval();
    dbms_output.put_line(xmldata);
    end;
    I passed above command in string variable for passing in code. like below
    string StrQry = "";
    StrQry += "declare \n";
    StrQry += "xmloutput xmltype; \n";
    StrQry += "xmldata varchar(32767); \n";
    StrQry += "teid t_id := t_en_id( 'L','L',NULL,12121,'ABC','USER','N'); \n";
    StrQry += "tgetinfo to_info := NULL; \n";
    StrQry += "RETVAL NUMBER; \n";
    StrQry += "begin \n";
    StrQry += "RETVAL := GET_DTLS(teid,tgetinfo); \n";
    StrQry += "dbms_output.put_line(RETVAL); \n";
    StrQry += "xmldata := tgetinfo.XML_info.getclobval(); \n";
    StrQry += "dbms_output.put_line(xmldata); \n";
    StrQry += "end; \n";
    I'm passing output parameter ":temp" for this command. I want to catch output xml in ":temp" variable
    which is varchar2 type.
    Added below line before printing xmldata;
    StrQry += ":temp := xmldata; \n";
    cn.Open();
    OracleCommand cmd = new OracleCommand(StrQry, cn);
    cmd.Parameters.Add(":temp", OracleDbType.Varchar2, 32767, ParameterDirection.Output);
    cmd.ExecuteNonQuery();
    But which executing through c# code it's showing the below error.
    Oracle.DataAccess.Client.OracleException ORA-06502: PL/SQL: numeric or value err
    or: character string buffer too small
    ORA-06512: at line 11 at Oracle.DataAccess.Client.OracleException.HandleError
    Helper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOp
    oSqlValCtx, Object src, String procedure)
    at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, Oracle
    Connection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx
    , Object src)
    at Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery()
    at orclGetDetails.Program.DoProcess()
    How can i get the output to output variable ":temp".
    Pls help.
    Ideas are appreciated.

    Your PL/SQL block isn't returning anything. DBMS_OUTPUT is, at best, a way of debugging code, not passing data around. Assuming you configure and enable an appropriately sized buffer, I suppose your ODP.Net application could make DBMS_OUTPUT.GET_LINE calls to retrieve the XML. But that's not not a way to design an application.
    If you want a PL/SQL block that returns XML, you'll need to create a function and have the function return the XML (you could also create a procedure with an OUT parameter in which you could return the XML).
    Justin

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

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

  • Select query inside PL/SQL block.

    Hello Experts,
    I am just a beginner with PL/SQL.
    If I write a select query from client like SQL dev and fire it against a database, it gives me result.
    Eg: select * from employee;
    Now when I use the same Query inside a PL/SQL block suppose:
    Declare
    begin
    select * from employee;
    end;
    This gives error on execution, mentioning that an INTO is expected etc...
    I have doubts here:
    1. Can't I use a plain select inside a PL/SQL block (if so why?)
    I know this is kind of very basic question, I tried searching this on the forum but could not find the thread, please redirect me to the link if this is already answered.

    user8578271 wrote:
    Hello Experts,
    I am just a beginner with PL/SQL.
    If I write a select query from client like SQL dev and fire it against a database, it gives me result.
    Eg: select * from employee;
    Now when I use the same Query inside a PL/SQL block suppose:
    Declare
    begin
    select * from employee;
    end;
    This gives error on execution, mentioning that an INTO is expected etc...
    I have doubts here:
    1. Can't I use a plain select inside a PL/SQL block (if so why?) Because when you run a query in a tool like SQL Developer, or SQL*Plus or TOAD etc. then it opens up a cursor and fetches the data into internal memory structures before displaying it, and that is all done automatically and hidden from you. In PL/SQL, there is no interface to display the results of SQL queries, so you have to specifically tell it what to put the data into.
    The syntax is (in basic terms)...
    SELECT column1, column2... columnX
    INTO variables or record structure
    FROM ...Though that can only select a single row of data. If your query returns more than 1 row it will give a TOO_MANY_ROWS exception. If your query returns no rows you will get a NO_DATA_FOUND exception.
    If you need to select multiple rows into variables etc., then you would need to "BULK COLLECT" into a collection/array structure, though this takes up valuable memory on the server and shouldn't be used unless necessary (and you understand the implications of doing it).

  • Q: how can i write PL/SQL block to check prerequisite?

    Hello...
    I designed an application using sql*plus statment for creating database and developer for creating forms, for on-line registration system for universties,
    so I need to teach me how can I write pl/sql block to check the prerequisite, taken courses, and complete hours for the students who wants register the courses via Internet.
    thanks alot in advance
    kindly send the answers a.s.a.p

    please repost this in the SQL & PL/SQL forum
    thanks - OTN

Maybe you are looking for