HOW TO EXECUTE A STORE PROCEDURE THAT RETURN MULTIPLE ROWS FROM A QUERY

I NEED TO CREATE AND USE A STORE PROCEDURE THAT IS GOING TO DO A SELECT STATEMENT AN THE RESULT OF THE SELECT STATEMENT IS RETURN IN SOME WAY TO THE REQUESTER.
THIS CALL IS MADE BY AN EXTERNAL LANGUAGE, NOT PL/SQL OR FORMS APPLICATION. USING FOR EXAMPLE ODBC AND VISUAL BASIC. WHAT I NEED TO DO IS ADD A DATA ACCESS LAYER TO MY APPLICATION AND I ALREADY HAVE IT DONE FOR MS SQL SERVER, BUT I NEED THE SAME FUNCTIONALITY ACCESSING AN ORACLE DATABASE.
FLOW:
1. VB CREATE A ODBC CONNECTION TO A ORACLE DATABASE
2. VB EXECUTE A STORE PROCEDURE
3. THE STORE PROCEDURE RETURNS TO THE VB APPLICATION THE RESULT OF THE QUERY THAT IS INSIDE OF THE STORE PROCEDURE.(I.E. THE STORE PROCEDURE IS A BASIC SELECT, NOTHING COMPLEX)
4. VB DISPLAY THE RESULT IN A GRID
FOR SURE I CAN DO THE SELECT DIRECTLY TO ORACLE, BUT FOR PERFORMANCE REASONS AND SCALABILITY, I'LL LIKE IT TO DO IT USING A STORE PROCUDURES
IS THIS POSIBLE?, HOW?
THANKS

Certainly, it's possible. First, define a stored procedure that includes an OUT parameter which is a REF CURSOR. Then, call the stored procedure via ODBC omitting the OUT parameter from the list of parameters. IT will automatically be returned as a result set. Syntax for both is below...
CREATE PROCEDURE foo (inParam in varchar2, resultSet OUT REF CURSOR )
In ODBC:
{call foo( 'someData' )}
Justin

Similar Messages

  • How to create a function that returns multiple rows in table

    Dear all,
    I want to create a funtion that returns multiple rows from the table (ex: gl_balances). I done following:
    -- Create type (successfull)
    Create or replace type tp_gl_balance as Object
    PERIOD_NAME VARCHAR2(15),
    CURRENCY_CODE VARCHAR2(15),
    PERIOD_TYPE VARCHAR2(15),
    PERIOD_YEAR NUMBER(15),
    BEGIN_BALANCE_DR NUMBER,
    BEGIN_BALANCE_CR NUMBER
    -- successfull
    create type tp_tbl_gl_balance as table of tp_gl_balance;
    but i create a function for return some rows from gl_balances, i can't compile it
    create or replace function f_gl_balance(p_period varchar2) return tp_tbl_gl_balance pipelined
    as
    begin
    return
    (select gb.period_name, gb.currency_code, gb.period_type, gb.period_year, gb.begin_balance_dr, gb.begin_balance_cr
    from gl_balances gb
    where gb.period_name = p_period);
    end;
    I also try
    create or replace function f_gl_balance(p_period varchar2) return tp_tbl_gl_balance pipelined
    as
    begin
    select gb.period_name, gb.currency_code, gb.period_type, gb.period_year, gb.begin_balance_dr, gb.begin_balance_cr
    from gl_balances gb
    where gb.period_name = p_period;
    return;
    end;
    Please help me solve this function.
    thanks and best reguard

    hi,
    Use TABLE FUNCTIONS,
    [http://www.oracle-base.com/articles/9i/PipelinedTableFunctions9i.php]
    Regards,
    Danish

  • Stored procedure that returns multiple tables

    Hello everyone,
    I was wondering if there's a way to write a stored procedure that returns multiple result set as in sql server. for example, in sql server, you can write 2 select statements and when loading them in c#, u can get two data tables.
    I am not sure having a single ref cursor for each select is the only solution. I might need to return a variable number of tables per procedure call (based on a certain criteria).
    Any ideas?
    thanks for your time

    Sure. Ref cursor is the only easier answer for your problem.
    satyaki>
    satyaki>select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    PL/SQL Release 10.2.0.3.0 - Production
    CORE    10.2.0.3.0      Production
    TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    Elapsed: 00:00:01.43
    satyaki>
    satyaki>create or replace procedure ref_gen_arg(choice in int,b in out sys_refcursor)
      2  is  
      3    str   varchar2(500);
      4  begin   
      5    if choice = 1 then      
      6      str := 'select * from emp';   
      7    elsif choice = 2 then      
      8      str := 'select * from dept';   
      9    end if;       
    10   
    11    open b for str;
    12  exception  
    13    when others then     
    14      dbms_output.put_line(sqlerrm);
    15  end;
    16  /
    Procedure created.
    Elapsed: 00:00:04.38
    satyaki>
    satyaki>
    satyaki>declare   
      2    rec_x emp%rowtype;   
      3    rec_y dept%rowtype;       
      4    w sys_refcursor;
      5  begin  
      6    dbms_output.enable(1000000);  
      7    ref_gen_arg(1,w);  
      8    loop    
      9      fetch w into rec_x;     
    10      exit when w%notfound;             
    11        dbms_output.put_line('Employee No: '||rec_x.empno||' - '||                          
    12                             'Name: '||rec_x.ename||' - '||                          
    13                             'Job: '||rec_x.job||' - '||                          
    14                             'Manager: '||rec_x.mgr||' - '||                          
    15                             'Joining Date: '||rec_x.hiredate||' - '||                          
    16                             'Salary: '||rec_x.sal||' - '||                          
    17                             'Commission: '||rec_x.comm||' - '||                          
    18                             'Department No: '||rec_x.deptno);  
    19    end loop;  
    20    close w;    
    21   
    22    ref_gen_arg(2,w);  
    23    loop    
    24      fetch w into rec_y;
    25      exit when w%notfound;            
    26         dbms_output.put_line('Department No: '||rec_y.deptno||' - '||                           
    27                              'Name: '||rec_y.dname||' - '||                           
    28                              'Location: '||rec_y.loc);  
    29    end loop;  
    30    close w;
    31  exception  
    32    when others then    
    33      dbms_output.put_line(sqlerrm);
    34  end;
    35  /
    Employee No: 9999 - Name: SATYAKI - Job: SLS - Manager: 7698 - Joining Date: 02-NOV-08 - Salary: 55000 - Commission: 3455 - Department No: 10
    Employee No: 7777 - Name: SOURAV - Job: SLS - Manager:  - Joining Date: 14-SEP-08 - Salary: 45000 - Commission: 3400 - Department No: 10
    Employee No: 7521 - Name: WARD - Job: SALESMAN - Manager: 7698 - Joining Date: 22-FEB-81 - Salary: 1250 - Commission: 500 - Department No: 30
    Employee No: 7566 - Name: JONES - Job: MANAGER - Manager: 7839 - Joining Date: 02-APR-81 - Salary: 2975 - Commission:  - Department No: 20
    Employee No: 7654 - Name: MARTIN - Job: SALESMAN - Manager: 7698 - Joining Date: 28-SEP-81 - Salary: 1250 - Commission: 1400 - Department No: 30
    Employee No: 7698 - Name: BLAKE - Job: MANAGER - Manager: 7839 - Joining Date: 01-MAY-81 - Salary: 2850 - Commission:  - Department No: 30
    Employee No: 7782 - Name: CLARK - Job: MANAGER - Manager: 7839 - Joining Date: 09-JUN-81 - Salary: 4450 - Commission:  - Department No: 10
    Employee No: 7788 - Name: SCOTT - Job: ANALYST - Manager: 7566 - Joining Date: 19-APR-87 - Salary: 3000 - Commission:  - Department No: 20
    Employee No: 7839 - Name: KING - Job: PRESIDENT - Manager:  - Joining Date: 17-NOV-81 - Salary: 7000 - Commission:  - Department No: 10
    Employee No: 7844 - Name: TURNER - Job: SALESMAN - Manager: 7698 - Joining Date: 08-SEP-81 - Salary: 1500 - Commission: 0 - Department No: 30
    Employee No: 7876 - Name: ADAMS - Job: CLERK - Manager: 7788 - Joining Date: 23-MAY-87 - Salary: 1100 - Commission:  - Department No: 20
    Employee No: 7900 - Name: JAMES - Job: CLERK - Manager: 7698 - Joining Date: 03-DEC-81 - Salary: 950 - Commission:  - Department No: 30
    Employee No: 7902 - Name: FORD - Job: ANALYST - Manager: 7566 - Joining Date: 03-DEC-81 - Salary: 3000 - Commission:  - Department No: 20
    Department No: 10 - Name: ACCOUNTING - Location: NEW YORK
    Department No: 20 - Name: RESEARCH - Location: DALLAS
    Department No: 30 - Name: SALES - Location: CHICAGO
    Department No: 40 - Name: LOGISTICS - Location: CHICAGO
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:00.73
    satyaki>Regards.
    Satyaki De.

  • The simplest way for plsql procedure to return multiple rows

    Hi,
    What is the simplest way for plsql procedure to return multiple rows (records). There are many combination of ways to do it but I am looking for a solution that is appropriate for plsql beginners. Many solutions use cursors, cursor variables, collections and more that kind of things that are complex on the face of it. Is it somehow possible to achieve the same with less effort?
    Sample query would be: SELECT * FROM EMPLOYEES;
    I want to use returned rows in APEX to compose APEX SQL(in that context plsql) report.
    It is enough to use just SELECT * FROM EMPLOYEES query in APEX but I want to use plsql procedure for that.
    Thank you!

    Hi,
    It depends :-).
    With +...that is appropriate for plsql beginners...+ in mind... it still depends!
    The list of techniques (ref cursors, cursor variables, collections, arrays, using explict SQL) you have referenced in your post can be made to work. but...
    +Is it somehow possible to achieve the same with less effort?+ Less effort : That needs to be defined (measured). Especially in the context of pl/sql beginners (who is a beginner?) .
    What is the level of "programming experience" ?
    What is the level of understanding of "Relational Result set" as processible in Oracle?
    If you are looking for
    Process_the_set_of rows_in APEX () kind of capabilitywhich "abstracts/hides" relation database from developers when working on relation database, it may not be the best approach (at least strategically). Because I believe it already is abstracted enough.
    I find REF CUROSOR most effective for such use, when the "begginer" has basic understanding of processing SQL result set .
    So in a nut shell, the techniques (that you already are familiar with) are the tools available. I am not aware of any alternative tools (in pure Oracle) that will simplify / hide basics from develpers.
    vr,
    Sudhakar B.

  • Custom database functoid to return multiple rows from database

    Hi,
    I have created a custom database fucntoid to execute a stored procedure which returns just single row from the database.But I could not manage to return multiple rows from the database.
    Does anyone know how to return multiple rows from DB and create a node with that many occurrences in the target schema?
    Thanks
    JB

    If you want to do this in messaging-only way without orchestration, then only option let to you is using .NET in BizTalk:
    We had similar requirement with one of our clients, where they didn’t want to use orchestration (though we emphasised on less impact orch would have compared to manageability),
    they still wanted to have pure-messaging only.
    We extended the custom XslTransform component that ships with BizTalk SDK (<BizTalk installation directory>\SDK\Samples\Pipelines\XslTransformComponent)).
    Created a custom disassembler pipeline component, used the XslTransform component from SDK to execute the map’s XSLT. Here we created a map with every links except the database ones. After executing the map, access the database, execute the store procedure
    which returns more than one row/dataset, enrich the XSLT transformed message with the dataset from your database in disassembler.
    Since .NET gives you the flexibility of access the dataset with more than one row, you can enrich the message in custom pipeline code.
    Other option is code the message transformation completely in .NET code in custom disassembler by passing the received message to method/code which would code the map/enrichment.
    While enriching you can execute the store procedure which returns more than one row/dataset, enrich the message further with the dataset from db.
    If this answers your question please mark it accordingly. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

  • JDeveloper + WebServices, RETURN multiple rows from pl/sql

    I need to return multiple rows from pl/sql procedure or function and publish it as a Web Service through JDeveloper.
    I try to use ref cursor, but then found that it is impossible to use ref cursor as a return value in Web Services.
    Please, help me to achieve result.

    Hello. I tried to make commands from article, but got errors
    "C:\Program Files\Java\jdk1.6.0_18\bin\java" -jar D:\oracle\Middleware\oracle_common\modules\oracle.webservices_11.1.1\wsa.jar -plsqlAssemble -appName Echo -sql wo -dataSource jdbc/OracleManagedDS -dbConnection jdbc:oracle:thin:@192.168.246.2:1521:data -dbUser syd/manager -style rpc -use encoded
    Error: Interface oracle.generated.wo: The class could not be loaded from the class path.

  • How to execute a store procedure automatically every day

    NT4 and Oracle816,I wanna let Oracle execute a store procedure automatically every day.
    How to do it?
    Thanx.

    There are a few ways to do this: first of all you could write a script file and schedule it to run every night.
    Another solution is to use the DBMS_JOB package. Refer to the documentation to see how it is used

  • (Resolved) How to execute 2 store procedures in 1 transaction?

    public void method1(){
    Session session = getSessionFactory().acquireSession();
    StoredProcedureCall spc1 = new StoredProcedureCall();
    spc1.setProcedureName("PKG.SP1");
    spc1.addNamedArgumentValue("param", "aaa");
    session.executeNonSelectingCall(spc1);
    StoredProcedureCall spc2 = new StoredProcedureCall();
    spc2.setProcedureName("PKG.SP2");
    spc2.addNamedArgumentValue("param", "bbb");
    session.executeNonSelectingCall(spc2);
    The above method is defined in SessionFacadeBean (just like the EJB in SRDemo),
    I want the above 2 procedures executed in 1 transaction. If spc2 failed, the spc1 should not be committed. How can I do this?
    Regards,
    Jason
    Message was edited by:
    Jason.Lu

    I have changed code as follows:
    public void method1(){
    DatabaseSession session = getSessionFactory().getSharedSession();
    session.beginTransaction();
    try{
    StoredProcedureCall spc1 = new StoredProcedureCall();
    spc1.setProcedureName("PKG.SP1");
    spc1.addNamedArgumentValue("param", "aaa");
    session.executeNonSelectingCall(spc1);
    StoredProcedureCall spc2 = new StoredProcedureCall();
    spc2.setProcedureName("PKG.SP2");
    spc2.addNamedArgumentValue("param", "bbb");
    session.executeNonSelectingCall(spc2);
    } catch (Exception e) {
    session.rollbackTransaction();
    } finally {
    session.release();
    It seems that the "session.rollbackTransaction();" will be executed if spc2 fails.
    toplink logs are as follows:
    [TopLink Info]: 2007.11.12 10:20:36.690--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--default login successful
    [TopLink Finer]: 2007.11.12 10:20:36.690--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--TX beginTransaction, status=NO_TRANSACTION
    [TopLink Finer]: 2007.11.12 10:20:36.705--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--TX Internally starting
    [TopLink Finer]: 2007.11.12 10:20:36.705--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--external transaction has begun internally
    [TopLink Fine]: 2007.11.12 10:20:36.705--ServerSession(1901666)--Connection(11203640)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--BEGIN PKG.SP1(param=>'aaa'); END;
    [TopLink Fine]: 2007.11.12 10:20:36.971--ServerSession(1901666)--Connection(22861541)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--BEGIN PKG.SP2(param=>'bbb'); END;
    [TopLink Warning]: 2007.11.12 10:20:37.440--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--Local Exception Stack:
    Exception [TOPLINK-4002] (Oracle TopLink - 10g Release 3 (10.1.3.3.0) (Build 070608)): oracle.toplink.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: ORA-06550: line 1, column 7:
    PLS-00306: wrong number or types of arguments in call to 'SP2'
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    Error Code: 6550
    [TopLink Finer]: 2007.11.12 10:20:37.455--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--TX rollbackTransaction, status=STATUS_ACTIVE
    [TopLink Finer]: 2007.11.12 10:20:37.455--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--TX Internally rolling back
    [TopLink Finer]: 2007.11.12 10:20:37.455--ServerSession(1901666)--Thread(Thread[HTTPThreadGroup-4,5,HTTPThreadGroup])--external transaction has rolled back internally
    The problem is the 1st store procedure is committed (it should be rolled back!), and the database is updated.
    Thanks,
    Jason

  • How to execute a store procedure

    Hi Forum
    I have a store procedure with 4 variables in and 1 out, how to execute via sql this procedure for obtain the result in the out variable.
    Thanks and regards

    SQL> create or replace procedure test_proc (
      2     a in number,
      3     b in number,
      4     c in number,
      5     d in number,
      6     outvar out number) is
      7  begin
      8     outvar := a + b + c + d;
      9* end;
    SQL> /
    Procedure created.
    SQL> variable var1 number;
    SQL> exec test_proc (1, 2, 3, 4, :var1);
    PL/SQL procedure successfully completed.
    SQL> print var1
          VAR1
            10
    SQL>

  • Returning Multiple Rows From DBAdapter Calling PL/SQL Procedure

    Oracle XE 10g Express Edition
    JDeveloper 11.1.1.2.0
    WebLogic Server 11g
    Guys,
    I have a table of orders, which I need to interrogate, and pass back any matching rows which meet certain criteria (e.g. status = 'OPEN').
    However, rather than create a DBAdapter using an Operation Type of "Peform an Operation on a Table/Select", I need to use an Operation Type of "Call a Stored Procedure or Function".
    I therefore need the procedure to return all the matching rows, rather than a single row.
    I have looked at declaring return parameters for the procedure of the following types:
    RECORD - is good because it allows me to return the elements of the row with their correct datatypes, but does not meet my needs because it will only support the return of a single row.
    VARRAY - good because it can contain many row elements, but not good because it only supports a single row return, and also because all elements of the VARRAY must be of the same data type.
    TABLE - good because it can contain many rows, but bad because each row can contain only two elements - the index element and the data element.
    I think I could first define a RECORD (to hold a row), and then define a TABLE, with the data element being the RECORD, but I have found JDeveloper very fussy indeed when dealing with 'non-standard' data types in the DBAdapter.
    Apologies if I am missing something obvious, but can anyone suggest a way of doing this?
    Many thanks in advance.
    Edited by: user2541290 on 17-Feb-2010 02:48

    Hi, I've been able to create process that seems to work. My platform is a but different but I don't think this is important for your question.
    Here is the PL/SQL code. Just make Db Adapter for Calling stored procedure and it returns all rows!
    Be aware of possible limitations on how manyrows you could return in one select! This can have severe impact on performance.
    Succes.
    Jos Baan
    CREATE OR REPLACE PACKAGE lab2_multiple_rows IS
    -- Author : 801455
    -- Created : 18-2-2010 8:05:52
    -- Purpose :
    -- Public type declarations
    TYPE rrows IS RECORD(
    mutdat DATE,
    opmerking VARCHAR2(20));
    TYPE trows IS TABLE OF rrows INDEX BY BINARY_INTEGER;
    -- Public constant declarations
    -- Public variable declarations
    -- Public function and procedure declarations
    FUNCTION retrows RETURN trows;
    END lab2_multiple_rows;
    CREATE OR REPLACE PACKAGE BODY lab2_multiple_rows IS
    -- Private type declarations
    -- Private constant declarations
    -- Private variable declarations
    -- Function and procedure implementations
    FUNCTION retrows RETURN trows IS
    lrows trows;
    lidx binary_integer := 1;
    BEGIN
    FOR rsql IN (SELECT t.* FROM jba_transactions t ORDER BY t.mutdat)
    LOOP
    lrows(lidx).mutdat := rsql.mutdat;
    lrows(lidx).opmerking := rsql.opmerking;
    lidx := lidx + 1;
    END LOOP;
    RETURN(lrows);
    END;
    BEGIN
    -- Initialization
    NULL;
    END lab2_multiple_rows;
    Edited by: Baan, Jos on 18-feb-2010 8:53

  • Need a function that return multiple rows

    Hi,
    I need a function that should return output of this query
    SELECT b.branding_code, c.name_desc
    FROM
    development.brandings b, godot.company c
    WHERE b.company_id = c.company_id;
    This above function return 30 rows and I am not giving any input
    Function using cursor,pipeline
    Please help
    Edited by: 1008783 on Jun 6, 2013 6:40 AM

    Hi,
    1008783 wrote:
    Hi
    Table A:
    Development schema
    branding_code,company_id
    Table B:
    Gogot schema
    name_desc,...Those aren't CREATE TABLE and INSERT statements. Post something that the people who want to help you can run on their own systems to re-create the problem and test their ideas.
    o/p:
    branding_code,name_desc
    I should return these column as o/p, no i/pA query will do that. There's no need for a function.
    There are good reasons for wanting a functin. If you have one, say what it is. Explain how you plan to use the function.
    Have you tried writing a function? Post your code. What specific problems did you have?

  • SQL query that returns exclusive rows from groups

    I'm using modified scott/tiger data for this.
    I've got two tables
    DEPT_X
    DEPTNO     DNAME             LOC     CODE_ID     SUB_DEPTNO
    10     ACCOUNTING     NEW YORK 111     101
    10     SALES             ATWN      111     102
    10     SALES             BTWN      112     103
    20     RESEARCH     DALLAS      111     201
    20     RESEARCH     CTWN      111     202
    30     SALES             CHICAGO      111     301
    40     OPERATIONS     BOSTON      112     401and
    BI_PD
    CODE_ID     PD_TYPE     BI_TYPE
    111          -1
    112     -1     I want to write a query that joins the code_ids of the two tables and lists out either only the records from DEPT_X who's code_ids have a -1 in the PD_TYPE(112) column and none of the code_ids that have a -1 in the BI_TYPE(111) column for each department or if that department has code_ids for the BI_TYPE (111), list out those rows. So for Deptno 10 you'll only get the row with SUB_DEPTNO = 103. But if for that DEPTNO= 20 you'll get the rows for both SUB_DEPTNO = 201 and 202.
    So the result should look like
    DEPTNO   SUB_DEPTNO
    10          103
    20          201
    20          202
    30          301.
    Basically I just want rows from each department that have code_ids = 111 not to show up if there are code_ids = 112.
    What is the query to do this?
    Message was edited by:
    user623359
    Message was edited by:
    user623359
    Message was edited by:
    user623359
    Message was edited by:
    user623359
    Message was edited by:
    user623359

    One way could be:
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP and Data Mining options
    SQL> with dept_x as (
      2      select 10 deptno, 'ACCOUNTING' dname, 'NEW YORK' loc, 111 code_id, 101 sub_deptno from dual
    union all
      3      select 10, 'SALES', 'ATWN', 111, 102 from dual union all
      4      select 10, 'SALES', 'BTWN', 112, 103 from dual union all
      5      select 20, 'RESEARCH', 'DALLAS', 111, 201 from dual union all
      6      select 20, 'RESEARCH', 'CTWN', 111, 202 from dual union all
      7      select 30, 'SALES', 'CHICAGO', 111, 301 from dual union all
      8      select 40, 'OPERATIONS', 'BOSTON', 112, 401 from dual ),
      9  --
    10  bi_pd as(
    11      select 111 code_id, null pd_type, -1 bi_type from dual union all
    12      select 112, -1, null from dual),
    13  --
    14  t as(
    15      select a.deptno,a.sub_deptno from dept_x a, bi_pd b
    16      where a.code_id = b.code_id and b.pd_type = -1)
    17  --
    18  select a.deptno,a.sub_deptno
    19  from dept_x a, bi_pd b
    20  where a.code_id = b.code_id and b.bi_type = -1 and 
    21        (a.deptno) not in (select deptno from t)
    22  union
    23  select deptno,sub_deptno from t;
        DEPTNO SUB_DEPTNO
            10        103
            20        201
            20        202
            30        301
            40        401

  • How to extract multiple rows from a query and how to populate it in form.

    hi all,
    i want to populate multiple rows fetched from a cursor into form.
    i write this procedur.
    PROCEDURE populate_q (v_t_no IN number) IS
    cursor ext_q is
    select test_question.no, q_no, text, ans1
    from test_question , question
    where test_question.no = v_t_no;
    BEGIN
    for ext_rec in ext_q loop
         :take_detail.q_no := ext_rec.q_no;
         :take_detail.question := ext_rec.text;
         EXIT WHEN ext_q%notfound;
         end loop;
    END;
    and call this WHEN-NEW-BLOCK-INSTANCE trigger.
    this don't return all the rows only the lost one is populated
    and shown in form.
    thanks
    Muhammad Nadeem
    Mardan (pakistan)
    [email protected]

    Hi,
    look for the next_record built-in. Your cusror only addresses the current row, but you want to create new rows as you go
    Frank

  • Need help with a SQL qurey that returns multiple rows for one record?

    I have the following query where I use a CASE WHEN clause to determine the date of a shift that begins with "FRLO" on day1 - day14 of the pay period. It works great if a schedule record contains one day that begins "FRLO", but if more than one day is "FRLO" then it only returns the first day it finds and not the others. Is there some way to get the query to return a ron for every day 1 - 14 that begins "FRLO"? System if Oracle 11G
    Order of the results is not important as this is part of a larger query that orders the results.
    Thanks in advance for any help,
    George
    SELECT s.empid,
    CASE
    WHEN UPPER (SUBSTR (s.Day1, 0, 4)) = 'FRLO'
    THEN
    pp.startpp
    WHEN UPPER (SUBSTR (s.Day2, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 1
    WHEN UPPER (SUBSTR (s.Day3, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 2
    WHEN UPPER (SUBSTR (s.Day4, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 3
    WHEN UPPER (SUBSTR (s.Day5, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 4
    WHEN UPPER (SUBSTR (s.Day6, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 5
    WHEN UPPER (SUBSTR (s.Day7, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 6
    WHEN UPPER (SUBSTR (s.Day8, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 7
    WHEN UPPER (SUBSTR (s.Day9, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 8
    WHEN UPPER (SUBSTR (s.Day10, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 9
    WHEN UPPER (SUBSTR (s.Day11, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 10
    WHEN UPPER (SUBSTR (s.Day12, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 11
    WHEN UPPER (SUBSTR (s.Day13, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 12
    WHEN UPPER (SUBSTR (s.Day14, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 13
    END
    startdate,
    NULL starttime,
    NULL endtime,
    8 hours,
    0 minutes
    FROM schedules s
    JOIN
    payperiods pp
    ON pp.periodid = s.periodid
    WHERE UPPER (SUBSTR (s.Day1, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day2, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day3, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day4, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day5, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day6, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day7, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day8, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day9, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day10, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day11, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day12, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day13, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day14, 0, 4)) = 'FRLO';
    CURRENT OUTPUT
    EMPID STARTDATE STARTTIME ENDTIME HOURS MINUTES
    753738, 3/25/2013 , , ,8 ,0
    753740, 3/25/2013 , , ,8 ,0
    753748, 3/25/2013 , , ,8 ,0
    DESIRED OUTPUT
    EMPID STARTDATE STARTTIME ENDTIME HOURS MINUTES
    753738, 3/25/2013 , , ,8 ,0
    753740, 3/25/2013 , , ,8 ,0
    753748, 3/25/2013 , , ,8 ,0
    753738, 3/26/2013 , , ,8 ,0
    753740, 3/26/2013 , , ,8 ,0
    753740, 3/28/2013 , , ,8 ,0
    753748, 1/1/2013 , , ,8 ,0
    753738, 4/3/2013 , , ,8 ,0
    753748, 4/3/2013 , , ,8 ,0
    CREATE TABLE SCHEDULES
    SCHEDULEID NUMBER(12) NOT NULL,
    EMPID NUMBER(12) NOT NULL,
    PERIODID VARCHAR2(6 BYTE) NOT NULL,
    AREAID NUMBER(12) NOT NULL,
    DAY1 VARCHAR2(50 BYTE),
    DAY2 VARCHAR2(50 BYTE),
    DAY3 VARCHAR2(50 BYTE),
    DAY4 VARCHAR2(50 BYTE),
    DAY5 VARCHAR2(50 BYTE),
    DAY6 VARCHAR2(50 BYTE),
    DAY7 VARCHAR2(50 BYTE),
    DAY8 VARCHAR2(50 BYTE),
    DAY9 VARCHAR2(50 BYTE),
    DAY10 VARCHAR2(50 BYTE),
    DAY11 VARCHAR2(50 BYTE),
    DAY12 VARCHAR2(50 BYTE),
    DAY13 VARCHAR2(50 BYTE),
    DAY14 VARCHAR2(50 BYTE),
    NOPTIND1 INTEGER DEFAULT 0,
    NOPTIND2 INTEGER DEFAULT 0,
    NOPTIND3 INTEGER DEFAULT 0,
    NOPTIND4 INTEGER DEFAULT 0,
    NOPTIND5 INTEGER DEFAULT 0,
    NOPTIND6 INTEGER DEFAULT 0,
    NOPTIND7 INTEGER DEFAULT 0,
    NOPTIND8 INTEGER DEFAULT 0,
    NOPTIND9 INTEGER DEFAULT 0,
    NOPTIND10 INTEGER DEFAULT 0,
    NOPTIND11 INTEGER DEFAULT 0,
    NOPTIND12 INTEGER DEFAULT 0,
    NOPTIND13 INTEGER DEFAULT 0,
    NOPTIND14 INTEGER DEFAULT 0
    CREATE TABLE PAYPERIODS
    PERIODID VARCHAR2(6 BYTE) NOT NULL,
    STARTPP DATE,
    ENDPP DATE
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693744, 753738, '082013', 2167, 'X',
    'FRLO<1530>', 'FRLO<1530>', '1530', '1530', '1530',
    'X', 'X', '1530', '1530', 'FRLO',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693745, 753740, '082013', 2167, 'X',
    'FRLO<1530>', 'FRLO<1530>', '1530', 'FRLO', '1530',
    'X', 'X', '1530', '1530', '1530',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693746, 753748, '082013', 2167, 'X',
    'FRLO<1530>', '1530', '1530', '1530', '1530',
    'X', 'X', 'FRLO<1530>', '1530', 'FRLO',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    COMMIT;
    Insert into PAYPERIODS
    (PERIODID, STARTPP)
    Values
    ('082013', TO_DATE('03/24/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;

    Do you have the opportunity to change the data model to have one day per row ? It would make this easier to get this result without the need for a 14-way CASE or UNION.
    If not...
    The case statement will return as soon as it matches one of the conditions. Since you want a match when any column in the row starts with FRLO you can use a UNION ALL treating each column as a separate result. There may be more efficient ways to do this, but here is one way:
    Select S.Empid,       Pp.Startpp Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day1, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+1 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day2, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+2 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day3, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+3 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day4, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+4 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day5, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+5 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day6, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+6 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day7, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+7 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day8, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+8 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day9, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+9 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day10, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+10 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day11, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+11 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day12, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+12 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day13, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+13 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day14, 0, 4)) = 'FRLO'

  • How to execute procedure returning data rows from sql plus

    Hi,
    I want to execute a stored procedure that returns data rows from sql plus. please let me know the syntax for the same.
    Thanks,
    YG

    user13065317 wrote:
    Even if i get the result set into the cursor, do i have to do normal fetch into all the coumn variables within a loop
    But suppose no of columns in my result set varies depending on a parameter to the stored procedure.
    Is there any straightforward way to retrieve all the data irrespective of no of columns in the result set.There is no such thing as a "+result set+". Oracle does not create a temporary data set in memory that contains the results of your query. What would happen if this result set is a million rows and is too large to fit into memory? Or there are a 100 clients each with a 100,000 row result set?
    This is not scalable. You will be severely limited in the number and sizes of these "+result sets+" that can be created in server memory.
    A cursor is in fact a "program" that is created by compiling the SQL source code that you provide. This source code is parsed and compiled into what Oracle calls an execution plan. This is nothing but a series of instructions that the cursor will execute in order to return the rows required.
    Thus the result set is actually the output from a cursor (a program). Likewise, bind variables are the input parameters to this program.
    All SQLs are parsed and compiled as cursors and stored in the SQL Shared Pool. Oracle gives you handle in return to use to address this cursor - bind values to it, execute it, describe the output structure returned by the cursor, and fetch the output from the cursor.
    On the client side, this handle is used in different ways. In PL/SQL alone, this cursor handle can be used as an implicit cursor (you do not even see or use the cursor handle in your PL/SQL code). Or you can use a PL/SQL cursor variable. Or a DBMS_SQL cursor variable. Or a reference cursor variable.
    Why so many different client structures for the very same SQL cursor handle returned by Oracle? Because to allow you, the programmer, all kinds of different features and flexibility.
    The ref cursor feature is the ability to pass this cursor handle around, not only between PL/SQL code, but also from PL/SQL to the actual client process (Java. VB, SQL*Plus, TOAD, etc).
    The primary thing to remember - irrespective of what the client calls this (e.g. ref cursor, SQL statement handle, etc), this all refers to the same SQL cursor in the Shared Pool. And that this SQL cursor is a program that outputs data, and not a result set in itself.

Maybe you are looking for