Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Return Single Row with Multiple Cols Instead of Multiple Row
Anil G wrote:
> Thank you for responses, the way i need output is single line > ABC Motorcycle On Road Casual ZX124 Red CA-Emision > > It is tough as the model returns those as multiple rows, cause this is > just the small section of SQL, actual output is giantic, it;s like > Total Orders, Forecasts etc by each of above column values.
The problem is that one cannot define a SQL projection with variable columns. The number of columns projected (returned) must be known up front.
So there are two basic choices here. Return a collection as a column. One column is returned, but it itself is a collection array (ADT or Advance Data Type).
The problem with this is that the client will need to know how to deal with an ADT column.
The second choice is to simply string the rows together into a single VARCHAR2 column. Easily done, but then you are limited to a VARCHAR2(4000) only. With an ATD there is not real limitation wrt the number of items in the collection.
The code below illustrates the second method. A single VARCHAR2 is returned, stringing all the rows (up to a maximum of a 100), together. Note that the REF CURSOR is expected to also only contain a single column - thus concat the columns in the REF CURSOR if needed.
Type created.
Type created.
Type created.
Package created.
Function created.
SQL> select object_name from all_objects where rownum < 11;
OBJECT_NAME
10 rows selected.
SQL> select * from TABLE( pipe1( CURSOR(select object_name from all_objects where rownum < 11) ) );
COL
.. and this returns a single row with a single column containing 10 rows' data.
This should give you some ideas on how to deal with your requirement in SQL. Keep in mind though that formatting is best dealt within the client - it is after all responsible for the presentation layer. So just to what extent formatting should be done at the SQL (and PL/SQL) level, must be considered.
-- BillyReceived on Thu Oct 27 2005 - 00:52:56 CDT