Return rows from pl-sql record type

We have a requirement to create function which returns cursor to java application. This cursor will have data from pl-sql record type.
Tried with pipelined function. I have written code below.
CREATE or replace PACKAGE test_pkg IS
    TYPE tp_rec IS RECORD(tt_id INTEGER,tt_text VARCHAR2(40));
    TYPE obj_tp_recs IS TABLE OF tp_rec;
    TYPE obj_tp_recs1 IS TABLE OF tp_rec;
    FUNCTION test_func RETURN tp_rec;
    function type_out return obj_tp_recs1 PIPELINED;
    PROCEDURE test_type (result out sys_refcursor);
END;
CREATE OR REPLACE PACKAGE BODY OMS.test_pkg IS
    FUNCTION test_func RETURN tp_rec
    AS
       currec tp_rec;
    BEGIN
       currec.tt_id := 1;
       currec.tt_text := 'test1';
    END;
     FUNCTION type_out RETURN obj_tp_recs1 PIPELINED
         AS
       currec1 test_pkg.tp_rec;
      begin
                currec1 := test_pkg.test_func;
                PIPE ROW(currec1);
                dbms_output.put_line(currec1.tt_id);
            end;
    PROCEDURE test_type (result out sys_refcursor)
    AS   
    BEGIN
              OPEN RESULT
              FOR SELECT * FROM TABLE(test_pkg.type_out());
    END;
END;
SQL> VARIABLE x REFCURSOR
SQL> exec test_pkg.test_type(:x);
PL/SQL procedure successfully completed.
SQL> print xThis code returns no data found exeception from function. How to achieve result 1 and test1 from above code?
Thanks in advance

SQL> VARIABLE x REFCURSOR
SQL> exec test_pkg.test_type(:x);
PL/SQL procedure successfully completed.
SQL> print x
ERROR:
ORA-06503: PL/SQL: Function returned without value
ORA-06512: at "SCOTT.TEST_PKG", line 8
ORA-06512: at "SCOTT.TEST_PKG", line 14
no rows selectedIf you look at test_func body it is missing return statement. Now:
SQL> CREATE OR REPLACE PACKAGE BODY test_pkg IS
  2      FUNCTION test_func RETURN tp_rec
  3      AS
  4         currec tp_rec;
  5      BEGIN
  6         currec.tt_id := 1;
  7         currec.tt_text := 'test1';
  8         RETURN currec;
  9      END;
10     
11       FUNCTION type_out RETURN obj_tp_recs1 PIPELINED
12       AS
13         currec1 test_pkg.tp_rec;
14        begin
15          currec1 := test_pkg.test_func;
16          PIPE ROW(currec1);
17          dbms_output.put_line(currec1.tt_id);
18          end;
19  
20      PROCEDURE test_type (result out sys_refcursor)
21      AS   
22      BEGIN
23        OPEN RESULT
24        FOR SELECT * FROM TABLE(test_pkg.type_out());
25       
26      END;
27  END;
28  /
Package body created.
SQL> exec test_pkg.test_type(:x);
PL/SQL procedure successfully completed.
SQL> print x
     TT_ID TT_TEXT
         1 test1
SQL> SY.

Similar Messages

  • 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 return rows from tmp table created inside function??

    Hi,
    I'm trying to return rows from a cursor or table (created within
    the function). How do I specify the return type?

    Hi,
    Here is the code that examples to create a function that can
    return data from table.This is achieved using REFCURSOR concept.
    Hope this helps you.I'm giving all stuff in single PL/SQL
    block.You can break it and create a package and declare
    refcursor type and function in that as well.
    DECLARE
    -- Declare generic cursor type
    TYPE gencur_type IS REF CURSOR ;
    -- Declare generic cursor varaible
    gencur gencur_type;
    -- Declare record type
    TYPE rec_type IS RECORD(descr emp.ename%type) ;
    -- Declare record type variable
    rec rec_type;
    --This how you declare a local function that returns gencrtype
    data set
    --This function is called in main block
    FUNCTION call_refcur return gencur_type
    AS
              rr gencur_type;
    BEGIN
         OPEN rr FOR SELECT descr FROM emp;
         RETURN rr;
    END;-- end of local function
    -- Main block begins here
    BEGIN
    -- Call the local function so that gencur will have
    -- the data set returned by select statement
    gencur:= call_refcur;
    -- Open a loop to test the stuff
    LOOP
         FETCH gencur INTO rec;
         EXIT WHEN gencur%NOTFOUND;
         null;
         dbms_output.put_line(rec.ename);
    END LOOP;
    END;
    Regards,
    Sridhar

  • PL/SQL Record Type to XMLType

    Hi,
    I wanted to convert a pl/sql record type automatically with just one command using xmltype.createXML but I am wondering if anyone out there has used it this way or whether it is possible. I can't find any examples anywhere, and I didn't really want to do the hard work of doing it one element at a time using xmlelement & xmlattributes :-).....
    e.g.,
    l_record emp%rowtype;
    l_xml xmltype;
    begin
    for l_record in (select * from emp)
    loop
    l_xml := xmltype.convertXML (l_record) ; --> is this possible?? I can do it on a cursor but it doesn't seem to like it if its a record type
    end loop;
    Thanks in anticipation.
    M
    Edited by: user12097147 on 3/11/2009 16:48

    You cannot pass just any record structure to a procedure and expects it to determine its structure and contents and give you XML in return.
    Also when you call XMLTYPE(), you are essentially instantiating an object - and calling the constructor method of that class. There are a number of constructors that have different parameter signatures.
    If you want XML from a table, then you should be using XML functions.. in the following fashion (there's a number of approaches you can use, depending on your requirements) :
    SQL> select xmlElement( "Employee", xmlForest(e.empno, e.ename,e.job) ) as XML from emp e order by e.empno;
    XML
    <Employee><EMPNO>7369</EMPNO><ENAME>SMITH</ENAME><JOB>CLERK</JOB></Employee>
    <Employee><EMPNO>7499</EMPNO><ENAME>ALLEN</ENAME><JOB>SALESMAN</JOB></Employee>
    <Employee><EMPNO>7521</EMPNO><ENAME>WARD</ENAME><JOB>SALESMAN</JOB></Employee>
    <Employee><EMPNO>7566</EMPNO><ENAME>JONES</ENAME><JOB>MANAGER</JOB></Employee>
    <Employee><EMPNO>7654</EMPNO><ENAME>MARTIN</ENAME><JOB>SALESMAN</JOB></Employee>
    <Employee><EMPNO>7698</EMPNO><ENAME>BLAKE</ENAME><JOB>MANAGER</JOB></Employee>
    <Employee><EMPNO>7782</EMPNO><ENAME>CLARK</ENAME><JOB>MANAGER</JOB></Employee>
    <Employee><EMPNO>7788</EMPNO><ENAME>SCOTT</ENAME><JOB>ANALYST</JOB></Employee>
    <Employee><EMPNO>7839</EMPNO><ENAME>KING</ENAME><JOB>PRESIDENT</JOB></Employee>
    <Employee><EMPNO>7844</EMPNO><ENAME>TURNER</ENAME><JOB>SALESMAN</JOB></Employee>
    <Employee><EMPNO>7876</EMPNO><ENAME>ADAMS</ENAME><JOB>CLERK</JOB></Employee>
    <Employee><EMPNO>7900</EMPNO><ENAME>JAMES</ENAME><JOB>CLERK</JOB></Employee>
    <Employee><EMPNO>7902</EMPNO><ENAME>FORD</ENAME><JOB>ANALYST</JOB></Employee>
    <Employee><EMPNO>7934</EMPNO><ENAME>MILLER</ENAME><JOB>CLERK</JOB></Employee>
    14 rows selected.

  • Dbms_xmlgen using pl-sql record type

    Hi
    I want to pass pl-sql record type and want to generate xml. Can dbms_xmlgen access pl-sql record type instead of query?
    OR please let me know any other package to pass pl-sql record type and generate XML.
    Thanks in advance

    Can dbms_xmlgen access pl-sql record type instead of query?Don't think so, but can't you pass the individual record components:
    SQL> declare
      type rec is record
        a   int,
        b   varchar2 (30)
      r     rec;
      ctx   int;
      x     xmltype;
    begin
      r.a := 1;
      r.b := 'Michael';
      ctx := dbms_xmlgen.newcontext ('select :x id, :y name from dual');
      dbms_xmlgen.setbindvalue (ctx, 'x', r.a);
      dbms_xmlgen.setbindvalue (ctx, 'y', r.b);
      x := dbms_xmlgen.getxmltype (ctx);
      dbms_output.put_line (x.getstringval ());
      dbms_xmlgen.closecontext (ctx);
    end;
    <ROWSET>
    <ROW>
      <ID>1</ID>
      <NAME>Michael</NAME>
    </ROW>
    </ROWSET>
    PL/SQL procedure successfully completed.?

  • Can you confirm for me please? - jdbc to pl/sql record types

    Hi, I was hoping somebody could confirm the following please? I've been researching this in the Oracle JDBC docs and the Internet, but would feel more comfortable if somebody would confirm my findings.
    I have a 10g database pl/sql procedure that takes a pl/sql record type as both IN and OUT parameter. I'm not allowed to modify this legacy procedure, though I may create new supporting code.
    My research shows there is no inherit support in JDBC for Oracle pl/sql record types as per the Oracle JDBC docs.
    As a solution, if the procedure only returned a record type, my understanding is I could create a ref cursor in pl/sql, as well as a wrapper procedure that calls my original procedure, and returns the record type through the ref cursor. This could then be used by my JDBC code as JDBC supports ref cursors as a return type.
    However in my case, as the record type is both an IN and OUT parameter of my procedure, my research so far says JDBC support for ref cursors does not allow the writing of value to the ref cursor to be submitted back to the database. Is this correct?
    If this limitation exists, as such the better (and only?) solution is to create a shadow pl/sql procedure that takes all the record elements as separate IN OUT parameters and then create a private record based on the record type and pass it to the original procedure.
    Is my research here correct? Any help appreciated.
    Thanks & regards,
    CM.

    Chris,
    As far as I know, PL/SQL record types are not supported in JDBC.
    I believe you may be able to use TopLink.
    I think Kuassi Mensah may have some examples in his book Oracle Database Programming.
    Alternatively, you could use an Oracle object instead of a PL/SQL record.
    This would be similar to what you are suggesting except that instead of a ref cursor, you would transfer the PL/SQL record to an Oracle object.
    Good Luck,
    Avi.

  • Any way to encode/decode PL/SQL record types ?

    Hello everyone,
    I came to XDK because I was looking for an easy way or working around a JDBC driver limitation. Namely that it does not access PL/SQL record types. I thought the best way was to encode my package's public types into XML streams and wrap up each public routine into a set of routines receiving and exporting data in XML.
    I installed XDK 9i/PL/SQL yesterday and tried it on my 8.1.6 db. So far so good.
    However, I did not see anything that does not access directly the database. The only preocupation of XSU is to access directly the DB for a variety of select, insert...
    Am I wrong ?
    I'm looking for a routine that takes an XML varchar, a DTD varchar and some description of my type (to map tags to fields) and parses the whole thing...
    Is there anything I could use to do that ???
    Thanks in advance...
    Alain

    Marwim wrote:
    You code should be instrumented. Whenever you need to debug/trace you switch it on and get a log fileor log table; it maybe a database parameter or simply a package variable, which you can set at runtime.
    This will allow you to debug a production environment where you should never be allowed to change the code or to use adebugger.And very valuable advice this is...
    Debugging needs to be part and parcel of a code unit (like a PL/SQL package), where you can execute it (in production or anywhere else) and tell it "+go forth, execute, and debug thyself+".

  • How to retrieve the values from PL/SQL table types.

    Hi Every one,
    I have the following procedure:
    DECLARE
    TYPE t1 IS TABLE OF emp%ROWTYPE
    INDEX BY BINARY_INTEGER;
    t t1;
    BEGIN
    SELECT *
    BULK COLLECT INTO t
    FROM emp;
    END;
    This procedure works perfectly fine to store the rows of employee in a table type. I am not able to retrieve the values from Pl/SQL table and display it using dbms_output.put_line command.
    Can anybody help me please!!!!!
    Thanks
    Ahmed.

    You mean, you can't add this
    for i in t.first..t.last loop
    dbms_output.put_line(t(i).empno||' '||t(i).ename||' '||t(i).job);
    end loop;or you can't add this
    set serveroutput onor maybe, you are working in third party application where dbms_output is not applicable at all?
    You see, not able like very similar it is not working - both are too vague...
    Best regards
    Maxim

  • Creating PL/SQL web services from PL/SQL records

    Hello
    Jdeveloper does not allow to create web services from pl/sql packages that use PL/SQL records.to do this,we have to use the jpublisher ?without using the jpublisher,if we create a webservice then the following error is displayed in the web service xml output file.
    <faultstring>Internal Server Error (Caught exception while handling request: java.rmi.RemoteException: java.sql.SQLException: ORA-06550: line 1, column 49: PLS-00181: unsupported preprocessor directive '$WS_SP_EVEN' )</faultstring>
    </env:Fault>
    Could any one suggest me, how to solve the above issue..?
    Regards
    Malathi

    Thank you, with db adapter it was working and also
    pl/sql web-services working successfully with object types.If we want to send the web-services to the client, do we need to send the entire folder that is created in the web-services folder of the external oc4j..?
    Creating the client process:
    we are using the wsdl file that is generated in the web-services and adding to the partner link to Invoke the operations of web-services. Is there any other way to invoke the webservices?Could any one please suggest me?
    Thanking you
    Malathi

  • How to delete a row from a SQL Server CE Table with multiple JOINs?

    I want to delete a record from a SQL Server CE table.
    There are 3 tables scripts, options and results. I would like to remove a record from the results table. The where clause contains dynamic information which retrieved via other queries to different tables in the same database. These queries work fine and deliver
    the desired data.
    The Compact server is a clone of a remote table created using the sync framework. The same query to the remote table works fine.
    The error I get is:
    There was an error parsing the query. [ Token line number = 1,Token line offset = 10,Token in error = from ]
    The code that throws the exception is as follows:
    Dim connLoc As SqlCeConnection = New SqlCeConnection(My.Settings.ConnectionString)connLoc.Open()     Dim strDel As String = "Delete r from ResultsTable r inner join OptionsTable o ON o.TestName=r.TestName inner join ScriptTable c ON r.TestName=c.TestName WHERE r.TestName = '" & ds1Loc.Tables(0).Rows(0)(1) & "' AND [Index] = '" & lstIndex & "'"Dim cmdDel As SqlCeCommand = New SqlCeCommandcmdDel.CommandText = strDelcmdDel.Connection = connLoccmdDel.ExecuteNonQuery()
    The values held in ds1Loc.Tables(0).Rows(0)(1) and lstIndex are
    correct so should not be the problem.
    I also tried using parameterised queries
    Dim strDel As String = "Delete r from [ResultsTable] r inner join [OptionsTable] o ON o.TestName=r.TestName inner join [ScriptTable] c ON r.TestName=c.TestName WHERE r.TestName = @TestName AND [Index] = @lstIndex"
    Dim cmdDel As SqlCeCommand = New SqlCeCommand        cmdDel.CommandText = strDel       
    With cmdDel.Parameters           
    .Add(New SqlCeParameter("@TestName", ds1Loc.Tables(0).Rows(0)(1)))           
    .Add(New SqlCeParameter("@lstIndex", lstIndex))       
    End With 
    cmdDel.Connection = connLoc        cmdDel.ExecuteNonQuery()
    I have tried replacing the "=" with "IN" in the the WHERE clause but this has not worked.
    Is it the join that is causing the problem? I can do a select with the same search criteria and joins from the same database.
    Also this query works with SQL Server. Is it perhaps that SQL CE does not support the Delete function the same as SQL Server 2008? I have been looking at this for a while now and cannot find the source of the error. Any help would be greatly appreciated.

    Hello,
    In SQL Server Compact, we can use join in FROM clause. The DELETE statement fail may be caused by the FOREIGN KEY constraint.
    Please refer to:
    DELETE (SQL Server Compact)
    FROM Clause (SQL Server Compact)
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • Get Number of rows from a sql query.

    I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
    Is there an easy way to do this without looping through the table?
    Thanks Jasper

    Hi Jasper,
    You can use the XPATH count function similar to  this:
    GetTagList.Results{count(/Rowsets/Rowset/Row)}
    Kind Regards,
    Diana Hoppe

  • Returning rows from stored proc

    Hello, this is the sample from the SQL Developer help to create proc:
    CREATE OR REPLACE
    PROCEDURE list_a_rating(in_rating IN NUMBER) AS
    matching_title VARCHAR2(50);
    TYPE my_cursor IS REF CURSOR;
    the_cursor my_cursor;
    BEGIN
    OPEN the_cursor
    FOR 'SELECT title
    FROM books
    WHERE rating = :in_rating'
    USING in_rating;
    DBMS_OUTPUT.PUT_LINE('All books with a rating of ' || in_rating || ':');
    LOOP
    FETCH the_cursor INTO matching_title;
    EXIT WHEN the_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(matching_title);
    END LOOP;
    CLOSE the_cursor;
    END list_a_rating;
    You have to write a cursor just to display results from a query. To me this is whacked but I'll go with it. So what if you wanted to return that resultset or refcursor so it can be consumed by a calling application or procedure? I tried something like
    CREATE OR REPLACE PROCEDURE list_a_rating(in_rating IN NUMBER, MatchingTitles OUT Ref Cursor)
    but that's not compiling. How do you return data from a proc or function?
    Thanks,
    Ken

    ktrock wrote:
    Hello, this is the sample from the SQL Developer help to create proc:A very poor example..
    You have to write a cursor just to display results from a query. To me this is whacked but I'll go with it. All SQL you send to Oracle for parsing and execution winds up as SQL cursors in the server's SQL shared pool. You, the client, then get a handle as reference to this cursor that was created for your SQL statement. Using this handle you can now fetch the output of the cursor and consume it on the client side.
    So there is no such thing as not using a cursor - you always use cursors. Many times it will be implicitly as you won't see the cursor workings as the client hides that from you.
    As for DBMS_OUTPUT and PL/SQL... PL/SQL cannot "write output". And time and again (stupid?) people will use DBMS_OUTPUT thinking that is "writing to the client's display device".
    It is not. It is writing data into a PL/SQL variable on the server. PL/SQL variable that consumes very expensive dedicated process memory on the server. The client (SQL-Developer, TOAD, SQL*Plus, etc) can read the contents of this variable and the client can render that contents on the client's display device.
    So how on earth does it make sense to take SQL data on the server (that is structured data and can vary significantly in volume), and write that as plain text into a PL/SQL variable?
    It does not make sense. At all. Bluntly put - it is just plain bloody stupid.
    So how do you return data using PL/SQL? The same way as you return data using SQL.
    PL/SQL allows the complexity of the SQL language, database model and so on to be moved from the client application into PL/SQL itself. The client thus no longer need to know table names, what to join where and when, how to write effective SQL and so on. It calls a PL/SQL procedure with optional parameters. This procedure has the logic to create the required SQL (based on the parameters too if applicable) and return the cursor handle for that SQL.
    The client thus calls PL/SQL, PL/SQL serves as the SQL abstraction layer and does the complex SQL bit, and PL/SQL then returns the SQL cursor handle to the client.
    In the PL/SQL language, there are different data types for dealing with the same SQL cursor handle - depending on what you want to do with that SQL cursor.
    If you want to pass that SQL cursor handle to a client, then you need to use the sys_refcursor data type in PL/SQL. Remember that SQL cursor does not know or care what data type the client (such as PL/SQL) uses to reference it. All SQL cursors in the server's shared pool are the same - how you treat them from a client side (as an explicit cursor, implicit cursor, ref cursor, etc) is a client program decision.
    Also keep in mind that a SQL cursor is NOT a result set. It is not a data set that is instantiated or created on the server that contains the data (or references to the data) for your SQL.
    If this was the case - just how many such cursor result sets could the server create before running out of memory?
    A cursor is an executable program. The source SQL code is compiled into a program called a cursor. This cursor contains the execution steps that the server needs to follow to find the data. The cursor then outputs this data as data is found. The execution plan of a cursor shows how this program looks like.

  • How to fetch rows from PL/SQL table(Collections)

    Hi,
    I retrived rows from the table and stored in to table type.
    Now I want fetch these rows and display on the screen. Pls guide me.
    following code is my code:
    DECLARE
    type t1 is table of emp%rowtype index by binary_integer;
    var1 t1;
    v_counter number:=0;
    BEGIN
    select * bulk collect into var1 from emp;
    for vr in var1.first..var1.last
    loop
    dbms_output.put_line(var1(1)); --Got an Error Here. Acually I don't Know how to  fetch.
    update dept set deptno=var1.deptno --Here also Error occured.
    end loop;
    END;

    Fetching rows to display them is a task for the client tool. You need to define a ref cursor therefore.
    If you just want to play around, here we go
    SQL> DECLARE
      2    type t1 is table of emp%rowtype index by binary_integer;
      3    var1 t1;
      4    v_counter number:=0;
      5  BEGIN
      6    select * bulk collect into var1 from emp;
      7    for vr in 1..var1.count loop
      8      dbms_output.put_line(var1(vr).ename);
      9      update dept set deptno=var1.deptno Here also Error occured.
    10    end loop;
    11  END;
    12  /
    SCOTT
    ADAMS
    PL/SQL procedure successfully completed.
    SQL>

  • Multiple return vals from PL/SQL

    Hello:
    I am calling a PL/SQL proc from Java, though the procedure has 2 output values it returns. Has anyone done something similar? I've tried an array with now success. Can you return a row with PL/SQL? Thus maybe use a RowIterator?

    You should look into a function/procedure that modifies its input arguments. Your java program can read those as well as the basic return value. That way you can have as many 'return values' as you need.

  • PL/SQL Record Type and Toplink

    Can i call PL/SQL procedure having a parameter of record type in toplink. If yes, how it is possible?

    Hi,
    Record defines a representation of a database row as field=>value pairs.Whenever a query is exceuted the rows in the result set can be accessed thru RECORD interface.Can you be more elaborate on the question.
    Are you trying to access the result set obtained after executing the Stored Proc using Record interface or you want to pass the Record as input parameter to Stored proc?

Maybe you are looking for

  • Error while trying to access system information for AS Java

    Hi, I´m not sure if this is the correct forum so if I'm wrong please let me know to move it to the correct one. We are facing an extrange behavior when trying to access System Information in an AS Java, every time we try to access the link the follow

  • PDF Download in IE 5.5

    I am facing a peculiar problem, regarding Saving PDF to client machine. I am trying to save the pdf file by popping up a Save As dialogue box. This works fine with IE 6.0 and IE 5.5 (basic), but in IE 5.5 SP1,2 the browser opens a Save as Dialogue bo

  • Credit Memo distribution issue..

    Strange Credit Memo Distribution In the distribution of credit memo, revenue posted 5 times for each line. can any one please help & let us know the reason. Thanks, Atul

  • Deconfirmation of Quantity in Sales Order

    In Sales Order, we confirm the quantity of Material.After confirming qty it gets reduced from our stock. Now i want to know is their any functionality avaiable in SAP by which we can deconfirm the confirmed quantity in Batch after a particular time.

  • Loading images / data in sequence - tilelist

    Hello! Is there a way to load first the first image then the next one.. and so on.. in the tilelist? As far as i can see, the images are loaded altogether. (Images are from the XML) thanks in advance!