Function returns a cursor

Hello all,
I have a function and inside the i used a cursor variable.
My values are coming to cursor variable and I want to return the particular cursor variable through function.
Plz anybody help me for the particular.
-Rupesh

Hello, Rupesh!
Try this example:
CREATE OR REPLACE FUNCTION func_cursor_variable(query VARCHAR2)
RETURN sys_refcursor IS
result sys_refcursor;
BEGIN
OPEN result FOR query;
RETURN(Result);
END func_cursor_variable;
Then launch SQL+ and run this query:
SELECT func_cursor_variable ( 'select sysdate from dual' ) FROM dual;

Similar Messages

  • Function returning a cursor - need help

    I created one function as below:
    CREATE OR REPLACE FUNCTION SAMPLE_TEST
    (IN_LOC_CD IN VARCHAR,IN_MAN_DT IN DATE,IN_RTE_NM IN VARCHAR)
         RETURN TYPES.REF_CURSOR
    AS
         STOP_RES_CURSOR TYPES.REF_CURSOR;
         V_IM_CNT NUMBER;
    BEGIN
         SELECT COUNT(*) INTO V_IM_CNT
         FROM PROFILE
         WHERE
              LOC_CD = IN_LOC_CD AND MAN_DT = IN_MAN_DT AND RTE_NM = IN_RTE_NM;
         IF v_IM_CNT = 0 THEN
              RAISE NO_DATA_FOUND;
         END IF;
         OPEN STOP_RES_CURSOR FOR
              SELECT IM.STOP_NBR,IM.ADDR_NBR,COUNT(IM.SUB_STOP_NBR)
              FROM INBOUND_MANIFEST IM
              WHERE
                   IM.LOCATION_CD = IN_LOCATION_CODE AND
                   IM.MANIFEST_DT = IN_MANIFEST_DATE AND
                   IM.ROUTE_NBR = IN_ROUTE_NUMBER
              GROUP BY
                   IM.STOP_NBR,IM.ADDR_NBR;
         RETURN STOP_RES_CURSOR;
    END;
    Now I want to test whether this function raises No data Found exception when there
    exists no data for LOC_CD,MAN_DT and RTE_NM in the table.
    How can I acheive this.
    Shall I create one more function and call this.
    Please tell me How it happens.
    Thanks in advance
    Regards
    Raghu

    do it inside the function some thing like this...
    SQL> create or replace function my_function return sys_refcursor
      2  as
      3     lCount integer;
      4     lRC sys_refcursor;
      5  begin
      6     select count(1)
      7       into lCount
      8       from dual
      9      where 1=2;
    10
    11     if lCount = 0
    12     then
    13             raise no_data_found;
    14     end if;
    15
    16     open lRC for select object_id
    17                    from all_objects
    18                   where object_id <= 10;
    19
    20     return lRC;
    21  exception
    22     when no_data_found then
    23             raise_application_error(-20001, 'Count returns 0');
    24  end;
    25  /
    Function created.
    SQL> var lRc refcursor
    SQL> exec :lRc := my_function
    BEGIN :lRc := my_function; END;
    ERROR at line 1:
    ORA-20001: Count returns 0
    ORA-06512: at "SYSADM.MY_FUNCTION", line 23
    ORA-06512: at line 1Thanks,
    Karthick

  • Custom report based on function returning cursor

    Hi,
    I'm trying to create a generic function that will be the basis of a custom report in Grid Control.
    The idea is to pass in a database link name, then the function builds a cursor to select using that name, and returns the result to Grid control to render.
    At the moment all I get is an empty table.
    My function is defined as follows:
    create or replace function list_databases(p_dblink varchar2) return sys_refcursor
    is
    db_name varchar2(40);
    c_cursor sys_refcursor;
    v_query varchar2(100);
    begin
    v_query := 'select name from rc_database@'||p_dblink;
    open c_cursor for v_query;
    return c_cursor;
    end;
    The custom report contains a 'Table from SQL' type, with the following sql statement:
    select sysman.list_databases('rmancat_test') from dual
    I've tried playing around with the ??EMIP_BIND_RESULTS_CURSOR?? bind variable but it only seems to work if I write the pl/sql block in the custom report, rather than going for a generic function.
    Any ideas are most welcome.
    Chris

    Why not use a simple procedure to achieve the same thing?

  • Call ref cursor functions in function also returning ref cursor?

    In 10g database, I have several functions and procedures, some packaged, that return ref cursors. These work as expected.
    Is there a way to call one or more of these in a new function and return another ref cursor?
    I know this doesn't work, but I hope it conveys the idea.
    function f_get_order_lines
       return sys_refcursor -- or defined ref cursor
    is
      v_ords orders_rcr;
      v_lines lines_rcr;
      x sys_refcursor;
    begin
      v_ords := f_get_orders;
      for rec in v_ords
       loop
        v_lines := f_get_lines;
       ... do other work here...
      end loop;
    return x;
    end;

    Pipelined table functions might do the trick for you, depending you your requirements and the transformations you need to make:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/tuning.htm#sthref2345

  • Function calling stored procedure that returns a cursor into a LOV

    Hello,
    Is it possible in HTML DB to implement a process that has a function that calls a stored procedure that returns a cursor, used to then populate a select list?
    Or can I do a function call to a stored procedure in the 'List of values definition' box for the item itself that returns a cursor to populate the item's select list?

    Hi Vikas,
    Actually, I just found another posting that shows how to do what I'm looking for:
    Re: Filling a LOV with a cursor
    Check it out. I posted another question in response to that discussion...maybe you could answer that? Thanks!
    Laura

  • Function returning cursor to be used in select statement

    I have written a function which returns a cursor. I want to use this in a select statement to display rows. How to do this.
    Eg : function - test_fn return sys_refcursor. (returns empno, empname, deptname)
    select test_fn from dual
    This should return
    empno empname deptno
    1 name1 dept1
    2 name 2 dept2
    ....

    Try This::
    SCOTT@xe_144>variable c  refcursor
    SCOTT@xe_144>CREATE OR REPLACE FUNCTION get_nm
      2     RETURN sys_refcursor
      3  AS
      4    
      5     c sys_refcursor;
      6  BEGIN
      7    
      8     open c for 'SELECT ENAME , SAL FROM EMP';
      9      
    10     RETURN c;
    11  END;
    12  /
    Function created.
    SCOTT@xe_144>select get_nm into :c from dual;
    GET_NM
    CURSOR STATEMENT : 1
    CURSOR STATEMENT : 1
    ENAME             SAL
    SMITH             800
    ALLEN            1600
    WARD             1250
    JONES            2975
    BLAKE            2850
    CLARK            2450
    KING             3000
    AKASH2           5000
    TURNER           1500
    ADAMS            1100
    JAMES             950
    MILLER           1300
    ABC              2000
    13 rows selected.Let me know if its works for you.

  • To_date function in procedure which returns a cursor type

    Hi All
    When i execute the following query ,it runs fine and i get required result also
    select *from a where adate>to_date('31-dec-2001')
    But When i use the same query in a procedure which returns a cursor i get a error
    "non numeric character was found where a numeric value was expected".
    Then i changed the query to
    select *from a where adate>to_date('31-dec-2001','dd-mon-yyyy') and it worked fine.
    As i know default date format is 'dd-mon-yyyy' and no need to give the date format
    explicitly in to_date function if the date(string) is in default format.
    Can any body tell me the reason behind it?
    Thanks
    Satya

    Given a comparison between a date and a string Oracle will convert the string to a date. This means that if you have an index on a string column and compare it to a date the index will not be available, but if you have an index ona date column and compare it to a string column the index will be available.
    create table t (string varchar2(30), d  date);
    insert into t values (to_char(sysdate), sysdate);
    commit;
    create index t_n1 on t(string);
    create index t_n2 on t(d);
    SQL> set autotrace traceonly
    SQL> select /*+ index t_n1 */
      2         *
      3    from t
      4   where string = sysdate;
    no rows selected
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   TABLE ACCESS (FULL) OF 'T'
    SQL> select /*+ index t_n2 */
      2         *
      3    from t
      4   where d = to_char(sysdate);
    no rows selected
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   TABLE ACCESS (BY INDEX ROWID) OF 'T'
       2    1     INDEX (RANGE SCAN) OF 'T_N2' (NON-UNIQUE)
    SQL>

  • How to get a stored function return value (which is not a cursor)?

    I want to do so with ADO inside Visual Basic. I know how to call a stored procedure, but nothing I tried could help me in calling a stored function and getting its return code. Note that the function has no cursors within its argument or return code.
    Your quick response would be appreciated
    Eyal

    Eyal,
    A stored function returns a value. You need to have the first parameter (of correct data type) for the returned value. The parameter binding is pretty much the same as you would do with stored procedures.
    e.g. "begin :1= proc(....); end;"
    Sinclair

  • MS Access, ODBC and SPs returning ref cursor

    I have some functions and procedures in a package returning ref cursors that work fine in my C++ batch applications. I would like for the GUI to be able to call these as well.
    Unfortunately, we are using Access as the front end (for now), and we have not been able to get the ref cursors to work. Is this supported? We are using Oracle 8.0.5 on Solaris, and our clients is Access 97 on NT using Intersolv's ODBC driver.
    My procedure looks something like:
    package jec is
    procedure open_sale_cur(
    p_client_id number,
    sale_curvar out sale_curtype)
    is begin
    open sale_curtype for select ... ;
    end;
    end;
    And the Access code looks like this:
    strSql = "begin jec.open_sale_cur(27, ?); end;"
    qdfTemp = conMain.CreateQueryDef("", strSql)
    qdfTemp.Parameters(0).Direction = dbParamOutput
    qdfTemp.Execute()
    This is the error when Execute() is called:
    PLS-00306: wrong number or types of arguments in call to 'OPEN_SALE_CUR'
    I am not an Access programmer; I am simply passing along this information. Any help would be greatly appreciated.

    We tried the {call...} syntax originally, but when we use it, we get an Oracle syntax error for the statement
    SELECT * FROM {call ...}
    Apparently Access prepends "SELECT..." before passing the SQL text to Oracle.
    This is also the case for procedures with normal scalars, such as numbers, returned as OUT values. When we use anon PL/SQL syntax and "?" placeholders with such procedures, they work. When we use {call ...} syntax or omit OUT placeholders, they do not.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Justin Cave ([email protected]):
    I suspect that you want to simply execute the statement
    strSql = {call open_sale_cur( 27 )}
    The ODBC driver will automatically figure out that there's an output parameter which is a ref cursor and return that as the result set. Note that you may want to download the latest 8.0.5 ODBC driver (8.0.5.10 I believe) if there are any problems.
    Justin<HR></BLOCKQUOTE>
    null

  • Problem with XSU when trying to execute pl/sql package returning ref cursor

    Hi,
    I'm exploring xsu with 8i database.
    I tried running sample program which I took from oracle
    documentation. Here is the details of these.
    ------create package returning ref cursor---
    CREATE OR REPLACE package testRef is
         Type empRef IS REF CURSOR;
         function testRefCur return empRef;
    End;
    CREATE OR REPLACE package body testRef is
    function testRefCur RETURN empREF is
    a empREF;
    begin
    OPEN a FOR select * from emp;
    return a;
    end;
    end;
    ---------package successfully created-----
    Now I use java program to generate xml data from ref cursor
    ------------java program ----------
    import org.w3c.dom.*;
    import oracle.xml.parser.v2.*;
    import java.sql.*;
    import oracle.jdbc.driver.*;
    import oracle.xml.sql.query.OracleXMLQuery;
    import java.io.*;
    public class REFCURt
    public static void main(String[] argv)
    throws SQLException
    String str = null;
    Connection conn = getConnection("scott","tiger"); //
    create connection
    // Create a ResultSet object by calling the PL/SQL function
    CallableStatement stmt =
    conn.prepareCall("begin ? := testRef.testRefCur();
    end;");
    stmt.registerOutParameter(1,OracleTypes.CURSOR); // set
    the define type
    stmt.execute(); // Execute the statement.
    ResultSet rset = (ResultSet)stmt.getObject(1); // Get the
    ResultSet
    OracleXMLQuery qry = new OracleXMLQuery(conn,rset); //
    prepare Query class
         try
    qry.setRaiseNoRowsException(true);
    qry.setRaiseException(true);
    qry.keepCursorState(true); // set options (keep the
    cursor alive..
         System.out.println("..before printing...");
    while ((str = qry.getXMLString())!= null)
    System.out.println(str);
         catch(oracle.xml.sql.OracleXMLSQLNoRowsException ex)
    System.out.println(" END OF OUTPUT ");
    qry.close(); // close the query..!
    // qry.close(); // close the query..!
    // Note since we supplied the statement and resultset,
    closing the
    // OracleXMLquery instance will not close these. We would
    need to
    // explicitly close this ourselves..!
    stmt.close();
    conn.close();
    // Get the connection given the user name and password..!
    private static Connection getConnection(String user, String
    passwd)
    throws SQLException
    DriverManager.registerDriver(new
    oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection
    ("jdbc:oracle:thin:@xxxx:1521:yyyy",user,passwd);
    return conn;
    when I ran the program after successful compilation,I got the
    following error
    ==========
    Exception in thread "main" oracle.xml.sql.OracleXMLSQLException:
    1
    at oracle.xml.sql.core.OracleXMLConvert.getXML(Compiled
    Code)
    at oracle.xml.sql.query.OracleXMLQuery.getXMLString
    (OracleXMLQuery.java:263)
    at oracle.xml.sql.query.OracleXMLQuery.getXMLString
    (OracleXMLQuery.java:217)
    at oracle.xml.sql.query.OracleXMLQuery.getXMLString
    (OracleXMLQuery.java:194)
    at REFCURt.main(Compiled Code)
    ============================
    Can anybody tell me why I'm getting this error.Am I missing any
    settings?
    thanks

    We are using 8.1.7 Oracle db with latest xdk loaded.
    am I missing any settings?

  • Function returning error - change notification

    I have a function returning error text. When error occurs I get the message
    'xx error has occurred' on the screen (in notification). Is there a way to control the message text so I can display different text?

    It's something like this:
    DECLARE l_code zip.code%TYPE;
    got_error varchar2(1) := 'N';
    l_check_fld varchar2(30000);
    l_error_fld varchar2(32000);
    vErrorFields varchar2(1000);
    CURSOR check_zip IS
    select ''
    from zip
    where code = l_code;
    BEGIN
    apex_collection.create_or_truncate_collection('ZIP');
    FOR i IN 1 .. apex_application.g_f03.COUNT LOOP
    vErrorFields := '';
    /* Code MUST be entered */
    if (apex_application.g_f03(i) is null and
    (apex_application.g_f04(i) is not null or
    apex_application.g_f05(i) is not null))then
    got_error := 'Y';
    vErrorFields := vErrorFields || ',f03';
    l_error_fld := l_error_fld || 'Row ' || to_char(i) || ':' ||' <span style="color: red">Code cannot be <strong>blank.</strong></span><br>';
    end if;
    END LOOP;
    if got_error = 'N' then
    apex_collection.delete_collection('ZIP');
    end if;
    RETURN l_error_fld;
    END;

  • Can Function Return more than One Values ??

    Hi Experts,
    I would like to ask you Can Function Return more than one values. I Used Function with Out and In out parameter and its working Fine..
    1. what is harm using Out and In out parameter in function
    2. if we can use Out and In out parameter in Function so what is deffernce between procedure and Function.
    3. Is there any Other Way Though which we can return more the One values in Function.
    Please advice me...
    Thanks
    Umesh Goel

    Yes/No.
    You can return multiple value from function. But, in PL/SQL and not in a SQL.
    The following examples demonstrate that -
    SQL*Plus: Release 9.2.0.1.0 - Production on Wed Mar 28 17:41:15 2007
    Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> create or replace package glob
      2  as
      3    b varchar2(20);
      4    c varchar2(20);
      5  end;
      6  /
    Package created.
    SQL>
    SQL> create or replace function test_mul_out(a in number)
      2  return number
      3  is
      4    cursor c1(eno in number)
      5    is
      6      select ename,job,sal
      7   from emp
      8   where empno = eno;
      9  
    10    rec c1%rowtype;
    11    d  number(10);
    12  begin
    13    open c1(a);
    14    loop
    15      fetch c1 into rec;
    16      exit when c1%notfound;
    17       glob.b:= rec.ename;
    18    glob.c:= rec.job;
    19    d:= rec.sal;
    20    end loop;
    21    close c1;
    22    return d;
    23  end;
    24  /
    Function created.
    SQL> set serveroutput on
    SQL>
    SQL> declare
      2    zz  number(10);
      3  begin
      4    select test_mul_out(7777)
      5    into zz
      6    from dual;
      7    
      8    dbms_output.put_line('Ename: '||glob.b);
      9    dbms_output.put_line('Job: '||glob.c);
    10    dbms_output.put_line('Sal: '||zz);
    11  end;
    12  /
    Ename: Avik
    Job: CLERK
    Sal: 3456
    PL/SQL procedure successfully completed.
    SQL> Regards.
    Satyaki De.

  • Function returning a set of records - how to do this?

    Hi,
    I need to return something of a table as a result:
    function Find_Item (vNUMBER in NUMBER(3,0)
    vNAME in VARCHAR2(200) ) return ???
    as
    begin
    SELECT UUID, Number, Name FROM Tab1
    Where NUMBER LIKE vNUMBER|| '%'
    intersect
    SELECT UUID, Number, Name FROM Tab1
    Where NAME LIKE vNAME|| '%' ;
    end;
    How to do this? Should I use a collection? Any ideas? I would be grateful for any hint.
    Best regards,
    Alicja

    user651039 wrote:
    And it would work if not for INTERSECT. So, I either I change the query or have to find walk-around.What is the issue with the INTERSECT? Why should the approach shown not work when using INTERSECT?
    Note that you can't define the parameters of your function like that, you have to specify the length of the varchar2 column in the calling code.
    If your query is going to return potentially a large number of rows you should consider using a "pipelined" function, as already mentioned, as the non-pipelined version could be consuming a lot of memory in case a large collection is going to be generated.
    Here's a sample of a pipelined table function:
    alter session set nls_language = 'AMERICAN';
    set echo on linesize 130 feedback 1
    drop type t_find_item_record_col force;
    drop type t_find_item_record force;
    drop function find_item;
    create or replace type t_find_item_record as object(
    uuid varchar2(30),
    a_number number,
    a_name varchar2(30)
    create or replace type t_find_item_record_col as table of t_find_item_record;
    create or replace function find_item(
    vNUMBER in NUMBER, vNAME in VARCHAR2) return t_find_item_record_col pipelined
    as
      a_find_item_record_col t_find_item_record_col;
      cursor c is
      select t_find_item_record(uuid, a_number, a_name) from (
      SELECT cast('a' as varchar2(30)) as UUID, 1 as a_Number, cast('b' as varchar2(30)) as a_Name
      FROM dual
      Where 35 LIKE vNUMBER|| '%'
      intersect
      SELECT 'a' as UUID, 1 as a_Number, 'b' a_Name FROM dual
      Where 'Bob' LIKE vNAME|| '%'
    begin
      open c;
      loop
        fetch c bulk collect into a_find_item_record_col limit 100;
        for i in 1..a_find_item_record_col.count loop
          pipe row(a_find_item_record_col(i));
        end loop;
        exit when a_find_item_record_col.count = 0;
      end loop;
      close c;
      return;
    end;
    select * from table(find_item(35, 'Bob'));Another option you might want to consider is using a REF CURSOR return type, so that you open the cursor accordingly in your function and return the opened cursor to the caller. The caller can then fetch from this cursor.
    And here's a sample function using ref cursor:
    create or replace function find_item_cursor(
    vNUMBER in NUMBER, vNAME in VARCHAR2) return sys_refcursor
    as
      c sys_refcursor;
    begin
      open c for
      SELECT cast('a' as varchar2(30)) as UUID, 1 as a_Number, cast('b' as varchar2(30)) as a_Name
      FROM dual
      Where 35 LIKE vNUMBER|| '%'
      intersect
      SELECT 'a' as UUID, 1 as a_Number, 'b' a_Name FROM dual
      Where 'Bob' LIKE vNAME|| '%';
      return c;
    end;
    variable a refcursor
    exec :a := find_item_cursor(35, 'Bob')
    print aHere's the output of the samples:
    Session altered.
    SQL>
    SQL> drop type t_find_item_record_col force;
    Type dropped.
    SQL>
    SQL> drop type t_find_item_record force;
    Type dropped.
    SQL>
    SQL> drop function find_item;
    Function dropped.
    SQL>
    SQL> create or replace type t_find_item_record as object(
      2  uuid varchar2(30),
      3  a_number number,
      4  a_name varchar2(30)
      5  );
      6  /
    Type created.
    SQL>
    SQL> create or replace type t_find_item_record_col as table of t_find_item_recor
    d;
      2  /
    Type created.
    SQL>
    SQL>
    SQL> create or replace function find_item(
      2  vNUMBER in NUMBER, vNAME in VARCHAR2) return t_find_item_record_col pipelin
    ed
      3  as
      4    a_find_item_record_col t_find_item_record_col;
      5    cursor c is
      6    select t_find_item_record(uuid, a_number, a_name) from (
      7    SELECT cast('a' as varchar2(30)) as UUID, 1 as a_Number, cast('b' as varc
    har2(30)) as a_Name
      8    FROM dual
      9    Where 35 LIKE vNUMBER|| '%'
    10    intersect
    11    SELECT 'a' as UUID, 1 as a_Number, 'b' a_Name FROM dual
    12    Where 'Bob' LIKE vNAME|| '%'
    13    );
    14  begin
    15    open c;
    16    loop
    17      fetch c bulk collect into a_find_item_record_col limit 100;
    18      for i in 1..a_find_item_record_col.count loop
    19        pipe row(a_find_item_record_col(i));
    20      end loop;
    21      exit when a_find_item_record_col.count = 0;
    22    end loop;
    23    close c;
    24    return;
    25  end;
    26  /
    Function created.
    SQL>
    SQL> select * from table(find_item(35, 'Bob'));
    UUID                             A_NUMBER A_NAME
    a                                       1 b
    1 row selected.
    SQL>
    SQL> create or replace function find_item_cursor(
      2  vNUMBER in NUMBER, vNAME in VARCHAR2) return sys_refcursor
      3  as
      4    c sys_refcursor;
      5  begin
      6    open c for
      7    SELECT cast('a' as varchar2(30)) as UUID, 1 as a_Number, cast('b' as varc
    har2(30)) as a_Name
      8    FROM dual
      9    Where 35 LIKE vNUMBER|| '%'
    10    intersect
    11    SELECT 'a' as UUID, 1 as a_Number, 'b' a_Name FROM dual
    12    Where 'Bob' LIKE vNAME|| '%';
    13    return c;
    14  end;
    15  /
    Function created.
    SQL>
    SQL> variable a refcursor
    SQL>
    SQL> exec :a := find_item_cursor(35, 'Bob')
    PL/SQL procedure successfully completed.
    SQL>
    SQL> print a
    UUID                             A_NUMBER A_NAME
    a                                       1 b
    1 row selected.Regards,
    Randolf
    Oracle related stuff:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle:
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • How to migrate sql server 2000 user defined function returns table

    Hi,
    How do I capture the SQL Server 200 user defined function that returns table? Is this supported in the current version of Oracle Migration Workbench? I am using the latest version - Release 9.2.0.1.0 with SQL SERVER 2000 plug-in.
    I was able to capture the SQL Server 2000 user defined function that returns string and smalldatetime but not the functions return table during the migrate data source stage.
    Thanks in Advance,
    Susan

    Susan,
    This is not currently supported. The next release of the Oracle Migration Workbench (due very soon), will do a better job of catching this mad reporting an error. We are looking into a suitable mapping and have created bug # 2355073 - TABLE DEFINITIONS NOT ACCEPTED FOR TABLE FUNCTIONS to track this issue.
    Once possible solution we are looking into is using the object type to emulate. Here is an example from the bug:
    Original table
    SQL> create table tabela (a number, b number, c number, d number);
    SQL> insert some values...
    SQL> select * from tabela;
    A B C D
    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 4 4 4
    SQL Server 2000 code
    CREATE FUNCTION FUNCRETORNATABELA()
    RETURNS TABLE
    AS
    RETURN SELECT A,B,C,D FROM TABELA
    SELECT A,B,C,D
    FROM FUNCRETORNATABELA()
    ORDER BY A
    Oracle code (workaround)
    SQL> create or replace type MyObjType as object (
    2 a number, b number, c number, d number);
    3 /
    Type created.
    SQL> create or replace type MyTabType as table of MyObjType;
    2 /
    Type created.
    SQL> create or replace function teste return Mytabtype pipelined as
    2 aa MyObjType := MyObjType(null, null, null, null);
    3 cursor c1 is select a,b,c,d from tabela;
    4 begin
    5 open c1;
    6 loop
    7 fetch c1 into aa.a, aa.b, aa.c, aa.d;
    8 exit when c1%NOTFOUND;
    9 pipe row (aa);
    10 end loop;
    11 close c1;
    12 return;
    13 end;
    14 /
    Function created.
    SQL> select * from table(teste);
    A B C D
    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 4 4 4
    SQL> select a, c from table(teste) order by c desc;
    A C
    4 4
    3 3
    2 2
    1 1
    Donal

  • ORA-06503: PL/SQL: Function returned without value

    Hello
    Having a bit of a problem with piplined functions.
    Why does this work :
    SET SERVEROUTPUT ON
    DECLARE
    TYPE SARRAY IS TABLE OF VARCHAR2(4000);
    CURSOR CU IS SELECT * FROM DX_XML_ATTENDANCE WHERE STUD_ID = 107777 AND BASE_ID = 94;
    T_STUD NUMBER(10);
    T_BASE NUMBER(10);
    T_DATE DATE;
    T_MARKS VARCHAR2(1000);
    LEN_MARKS NUMBER;
    PDATE DATE;
    SDATE DATE;
    EDATE DATE;
    SLEN NUMBER;
    WEEKLEN NUMBER;
    INIPOS NUMBER;
    MARRAY VARCHAR2(1000);
    SUBARRAY SARRAY := SARRAY();
    SFILL VARCHAR2(14) := '--------------';
    EPOS NUMBER;
    MY_REC     DX_XML_ATTENDANCE%ROWTYPE;
    BEGIN
    SUBARRAY.EXTEND(17);
    DBMS_OUTPUT.ENABLE(100000000);
    --FOR MY_REC IN CU
    OPEN CU;
    LOOP
         FETCH CU INTO MY_REC;
         EXIT WHEN (CU%NOTFOUND);
    T_STUD := MY_REC.STUD_ID;
    T_BASE := MY_REC.BASE_ID;
    T_DATE := TO_DATE(MY_REC.START_DATE, 'DD/MM/YYYY');
    T_MARKS := MY_REC.MARKS;
    LEN_MARKS := LENGTH(T_MARKS);
    EPOS := LEN_MARKS / 2;
    SDATE := ROUND(TO_DATE(T_DATE), 'W') - 1;
    INIPOS := TO_NUMBER(TO_CHAR(T_DATE, 'D'));
    SLEN := INIPOS + 3;
    PDATE := SDATE;
    EDATE := SDATE + EPOS;
    MARRAY := SUBSTR(T_MARKS, 1, SLEN);
    WEEKLEN := LENGTH(MARRAY);
    IF WEEKLEN < 14 THEN
         MARRAY := SUBSTR(SFILL, 1, 14 - WEEKLEN) || MARRAY;
    END IF;
    SUBARRAY(1) := T_STUD;
    SUBARRAY(2) := T_BASE;
    SUBARRAY(3) := PDATE;
    FOR i IN 4 .. 17 LOOP
         SUBARRAY(i) := SUBSTR(MARRAY, i - 3, 1);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(SUBARRAY(1)||' '||SUBARRAY(2)||' '||SUBARRAY(3)||' '||SUBARRAY(4)||' '||
         SUBARRAY(5)||' '||SUBARRAY(6)||' '||SUBARRAY(7)||' '||SUBARRAY(8)||' '||SUBARRAY(9)||' '||
         SUBARRAY(10)||' '||SUBARRAY(11)||' '||SUBARRAY(12)||' '||SUBARRAY(13)||' '||SUBARRAY(14)||' '||
         SUBARRAY(15)||' '||SUBARRAY(16)||' '||SUBARRAY(17));
    WHILE PDATE < EDATE LOOP
         PDATE := PDATE + 7;
         MARRAY := SUBSTR(T_MARKS, SLEN + 1, 14);
         WEEKLEN := LENGTH(MARRAY);
         IF WEEKLEN < 14 THEN
              MARRAY := MARRAY || SUBSTR(SFILL, 1, 14 - WEEKLEN);
         END IF;
         FOR i IN 4 .. 17 LOOP
              SUBARRAY(i) := SUBSTR(MARRAY, i - 3, 1);
         END LOOP;
         SUBARRAY(3) := PDATE;
    DBMS_OUTPUT.PUT_LINE(SUBARRAY(1)||' '||SUBARRAY(2)||' '||SUBARRAY(3)||' '||SUBARRAY(4)||' '||
         SUBARRAY(5)||' '||SUBARRAY(6)||' '||SUBARRAY(7)||' '||SUBARRAY(8)||' '||SUBARRAY(9)||' '||
         SUBARRAY(10)||' '||SUBARRAY(11)||' '||SUBARRAY(12)||' '||SUBARRAY(13)||' '||SUBARRAY(14)||' '||
         SUBARRAY(15)||' '||SUBARRAY(16)||' '||SUBARRAY(17));
         PDATE := PDATE + 7;
         SLEN := SLEN + 14;
    END LOOP;
    END LOOP;
    END;
    and this does not :
    CREATE OR REPLACE PACKAGE BODY PARSE_ATTENDANCE AS
    FUNCTION ENUM_MARKS(SEL_SQL IN VARCHAR2)
    RETURN TMP_ATT_DATA_TBL PIPELINED
    IS
    V_SQL           VARCHAR(1000):= SEL_SQL;
    V_CURSOR      SYS_REFCURSOR;
    V_ROW          TMP_ATT_HOLDING:=TMP_ATT_HOLDING(NULL, NULL, NULL, NULL);
    T_STUD           NUMBER(10);
    T_BASE           NUMBER(10);
    T_DATE           DATE;
    T_MARKS      VARCHAR2(1000);
    LEN_MARKS      NUMBER;
    PDATE          DATE;
    SDATE          DATE;
    EDATE          DATE;
    SLEN           NUMBER;
    WEEKLEN      NUMBER;
    INIPOS           NUMBER;
    MARRAY           VARCHAR2(1000);
    SUBARRAY      SARRAY := SARRAY();
    SFILL           VARCHAR2(14) := '--------------';
    EPOS           NUMBER;
    BEGIN
    SUBARRAY.EXTEND(17);
    OPEN V_CURSOR FOR V_SQL;
    LOOP
         FETCH V_CURSOR INTO V_ROW.STUD_ID, V_ROW.BASE_ID, V_ROW.START_DATE, V_ROW.MARKS;
         EXIT WHEN V_CURSOR%NOTFOUND;
    T_STUD := V_ROW.STUD_ID;
    T_BASE := V_ROW.BASE_ID;
    T_DATE := TO_DATE(V_ROW.START_DATE, 'DD/MM/YYYY');
    T_MARKS := V_ROW.MARKS;
    LEN_MARKS := LENGTH(T_MARKS);
    EPOS := LEN_MARKS / 2;
    SDATE := ROUND(TO_DATE(T_DATE), 'W') - 1;
    INIPOS := TO_NUMBER(TO_CHAR(T_DATE, 'D'));
    SLEN := INIPOS + 3;
    PDATE := SDATE;
    EDATE := SDATE + EPOS;
    MARRAY := SUBSTR(T_MARKS, 1, SLEN);
    WEEKLEN := LENGTH(MARRAY);
    IF WEEKLEN < 14 THEN
         MARRAY := SUBSTR(SFILL, 1, 14 - WEEKLEN) || MARRAY;
    END IF;
    SUBARRAY(1) := T_STUD;
    SUBARRAY(2) := T_BASE;
    SUBARRAY(3) := PDATE;
    FOR i IN 4 .. 17 LOOP
         SUBARRAY(i) := SUBSTR(MARRAY, i - 3, 1);
    END LOOP;
    PIPE ROW(TMP_ATT_DATA_OBJ(SUBARRAY(1),SUBARRAY(2),SUBARRAY(3),SUBARRAY(4),
         SUBARRAY(5),SUBARRAY(6),SUBARRAY(7),SUBARRAY(8),SUBARRAY(9),
         SUBARRAY(10),SUBARRAY(11),SUBARRAY(12),SUBARRAY(13),SUBARRAY(14),
         SUBARRAY(15),SUBARRAY(16),SUBARRAY(17)));
    WHILE PDATE < EDATE LOOP
         PDATE := PDATE + 7;
         MARRAY := SUBSTR(T_MARKS, SLEN + 1, 14);
         WEEKLEN := LENGTH(MARRAY);
         IF WEEKLEN < 14 THEN
              MARRAY := MARRAY || SUBSTR(SFILL, 1, 14 - WEEKLEN);
         END IF;
         FOR i IN 4 .. 17 LOOP
              SUBARRAY(i) := SUBSTR(MARRAY, i - 3, 1);
         END LOOP;
         SUBARRAY(3) := PDATE;
         PIPE ROW(TMP_ATT_DATA_OBJ(SUBARRAY(1),SUBARRAY(2),SUBARRAY(3),SUBARRAY(4),
         SUBARRAY(5),SUBARRAY(6),SUBARRAY(7),SUBARRAY(8),SUBARRAY(9),
         SUBARRAY(10),SUBARRAY(11),SUBARRAY(12),SUBARRAY(13),SUBARRAY(14),
         SUBARRAY(15),SUBARRAY(16),SUBARRAY(17)));
         PDATE := PDATE + 7;
         SLEN := SLEN + 14;
    END LOOP;
    END LOOP;
    END ENUM_MARKS;
    END PARSE_ATTENDANCE;
    (This is then called like SELECT * FROM
    TABLE(
    PARSE_ATTENDANCE.ENUM_MARKS(
    'SELECT STUD_ID, BASE_ID, START_DATE, MARKS
    FROM DX_XML_ATTENDANCE WHERE STUD_ID = 107777
    AND BASE_ID = 94'))
    I get the same error, around this section near the bottom :
         PDATE := PDATE + 7;
         SLEN := SLEN + 14;
    Can any one help?

    Here is an example. you are missing an return statement.
    SQL> create or replace type varchar2_table is table of varchar2(10) ;
      2  /
    Type created.
    SQL> show errors
    No errors.
    SQL> create or replace function get_data return varchar2_table pipelined is
      2  begin
      3      pipe row(('Test')) ;
      4  end ;
      5  /
    Function created.
    SQL> show errors
    No errors.
    SQL> select * from table(get_data) ;
    ERROR:
    ORA-06503: PL/SQL: Function returned without value
    ORA-06512: at "KKISHORE.GET_DATA", line 3
    no rows selected
    SQL> create or replace function get_data return varchar2_table pipelined is
      2  begin
      3      pipe row(('Test')) ;
    4 return ;
      5  end ;
      6  /
    Function created.
    SQL> show errors
    No errors.
    SQL> select * from table(get_data) ;
    COLUMN_VAL
    Test
    SQL>

Maybe you are looking for

  • Studio Monitors - what to look for...

    I'm going to say this right now - I'm NOT asking "hey, whats the best studio monitors I can get for x dollars." That's not what I'm interested in. What I AM interested in is what TO look for, and what NOT to look for in buying a new set of stereo mon

  • Please help a debug

    Hello, make a pl/sql, get an error --first, create a function to find days intervals between dates create or replace function test_schema.find_interval(from_date in date, to_date in date) return number as begin return abs(trunc(to_date) - trunc(from_

  • SP01 file save as option disabled

    Hello Gurus,      In SP01 there is a option "Save as local file" to save spool request as file to .html or .trf file. That is not enable for invoice spool request,  this report is developed in smart froms by ABAP developers.     How can i save this r

  • I am running an Applescript to determine if an application exists and receiving an error related to sandboxing - why

    I am attempting to write a script to do some file conversion when I import files into Aperture using Aperutre's importAcrtionFromVersions event.  Right now I am simply trying to have the script make sure the conversion application exists and I am fin

  • Won't start up correctly, but gets to finder

    Ok so after having read a bunch of forums i thought i would throw up my problem since it seems a little different than most i have seen thus far. My iBook starts correctly, but once it gets to the finder nothing works, the dock loads and all of the i