Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: How to accessing columns of a query-result by index
Hi folks,
many thanks for your advices !
I've solved the problem with dbms_sql. Now I am able to work with a resultset without having any informations about it's structure before execution. I tested it several times, it seems to work. I cannot say anything about performance and scalability this time, but in this case, I need it to work with resultset with less then 1000 rows.
An implementation in Java would have worked as well, but there's no other use of java in the application, so I decided to use pl/sql.
for anybody who cares, here's the code.
I was too lazy to strip it down, so it contains stuff, which is only
needed in my application - but time is money ;-)
greets, Jens
create or replace package Dynamic is
type typColContent is table of typ_Content;
type typRows is table of typColContent;
end Dynamic;
/
create or replace package body Dynamic is
tabColTypes typColTypes; -- Mappingtable for supported
Datatypes (see initalization)
null_content typ_Content; -- used for resetting
variables of type typ_Content;
dynCursor integer; -- Handle for dynamic cursor tabDescription dbms_sql.desc_tab; -- Oracle-defined description for a column rec_Content typ_Content; -- contains Informationsabout each value of the resultset
tabColDesc typColDesc := typColDesc(); -- contains my own column-description (subset of desc_tab)
tabColContent typColContent := typColContent(); -- column-values and -descriptions of one single row
tabRows typRows := typRows(); -- contaains the whole resultset
nColCnt integer;
nColNum integer;
ignore number;
nRowCount number;
dBuffer date;
cBuffer varchar2(100);
nBuffer number;
begin
while nColNum is not null loop
rec_Content := null; rec_Content.ColType := tabColTypes(tabDescription(nColNum).col_type); rec_Content.ColName := tabDescription(nColNum).col_name; rec_Content.ColLen := tabDescription(nColNum).col_max_len; rec_Content.ColPrec := tabDescription(nColNum).col_precision; rec_Content.ColScale := tabDescription(nColNum).col_scale; tabColDesc.extend; tabColDesc(nColNum) := rec_Content; nColNum := tabDescription.next(ncolNum); end loop; -- Use the description for defining columns (needs to be done before fetching) nColNum := tabColDesc.first; while nColNum is not null loop case tabColDesc(nColNum).colType when 'N' THEN dbms_sql.define_column(DynCursor,nColNum, nBuffer); when 'D' THEN dbms_sql.define_column(DynCursor,nColNum, dBuffer); when 'C' THEN dbms_sql.define_column(DynCursor,nColNum, cBuffer, tabColDesc(nColNum).ColLen); end case; nColNum := tabColDesc.next(ncolNum); end loop; -- no need for additional execute (i am astonished...) -- ignore := dbms_sql.execute(dynCursor); -- Examining the resultset nRowCount := 0; loop tabColContent := typColContent(); if dbms_sql.fetch_rows(dynCursor) > 0 then nColNum := tabColDesc.first; while (nColNum is not null) loop tabColContent.extend; tabColContent(nColNum) := tabColDesc(nColNum); case tabColDesc(nColNum).colType when 'N' THEN dbms_sql.column_value(dynCursor,nColNum,nBuffer); tabColContent(nColNum).cValue := to_char(nBuffer); when 'D' THEN dbms_sql.column_value(dynCursor,nColNum,dBuffer); tabColContent(nColNum).cValue := to_char(dBuffer); when 'C' THEN dbms_sql.column_value(dynCursor,nColNum,cBuffer); tabColContent(nColNum).cValue := cBuffer; end case; nColNum := tabColDesc.next(ncolNum); end loop; nRowCount := nRowCount + 1; tabRows.extend; tabRows(nRowCount) := tabColContent; else exit; end if; end loop; -- done... dbms_sql.close_cursor(dynCursor); return tabRows;
end ExecuteQuery;
begin
end Dynamic;
/
Received on Wed Sep 21 2005 - 10:26:34 CDT