print table row contents to scree

I'm new to PL/SQL and I'm trying to print the contents of a specific table row using a PL/SQL procedure. The tricky part is that I want to make the procedure generic enough so that it can print the contents of ANY table. There shouldn't be any hard coding of column types.

Here is some pseudo code (NOT WORKING!!!) of what it is that I'm trying to do:

CREATE OR REPLACE PROCEDURE print_row(table_name IN VARCHAR2(50), table_id IN number) IS
  currdata  SYS.AnyData;
BEGIN
  FOR i < columnCount LOOP
    SELECT columnName INTO currdata FROM table_name WHERE table_id = table_id;
    print_any(currdata);
  END LOOP;
END print_row;
/

Open in new window


Unfortunately you cannot SELECT INTO a SYS.AnyData variable, which I though was the purpose of AnyData - that it can take ANY data of ANY data type. The print_any (data IN SYS.AnyData) procedure already exists and works. But I really need help with the print_row procedure. It seems such a simple task so there must be something I'm missing...

Tino
nirvanastorageAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Just to the screen: dbms_output.put_line(columnname);

dbms_output is buffered and flush when the process is complete.

in sqlplus you need to make sure to: set serveroutput on
0
nirvanastorageAuthor Commented:
I know the dbms_output.put_line but how can it be used to print an entire table row with all its columns to the screen - without hardcoding all the column names?
0
slightwv (䄆 Netminder) Commented:
How do you want the row data displayed?  CSV, fixed length, ???

How do you plan on handling date columns?

You can probably pull this off with an XML trick similar to:
http://www.experts-exchange.com/Database/Oracle/Q_24914739.html?#25864822

I would just like a little more info before I work on the test code.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nirvanastorageAuthor Commented:
The format can be CSV.

In the tables I'm concerned about there are no "date" types but only "timestamp with time zone" types. However, the output format is not important - as long as it prints something I can change the format setting the nls_timestamp_tz_format session variable.

The other column types I'm encountering are mostly varchar2 and number.
0
slightwv (䄆 Netminder) Commented:
I apologize for not getting to this until now.  Got tied up.

What version of Oracle does this need to run on?

I'm sure I can come up with something but need to know what version it needs to be compatible with.
0
nirvanastorageAuthor Commented:
11g R2
0
slightwv (䄆 Netminder) Commented:
Hopefully this can wait a while or another Expert will show up later.

The bad news is I don't have access to 11g right now and what I want to do doesn't work with my 10g XE database I have at home.

If you don't have a solution by tomorrow, I'll work on one for you when I get back to my development database.
0
nirvanastorageAuthor Commented:
10g XE is fine; It's very unlikely that the PL/SQL syntax would have changed in 11g
0
slightwv (䄆 Netminder) Commented:
Sorry but, I want to use XML functions not available in XE.  They use Java.

I might be able to kludge something together in XE but it would be very ugly.
0
nirvanastorageAuthor Commented:
Ugly is OK. This is mainly for debugging purposes - not production.
0
slightwv (䄆 Netminder) Commented:
Try this.  Since it isn't production and obviously doesn't have to be as efficient as it could be nor does it appear you need a defined output, I came up with this.

Who knows, it might be quicker what what I was planning?
drop table tab1 purge;
create table tab1(col1 char(1), col2 char(1));
insert into tab1 values('a','b');
insert into tab1 values('c','d');

drop table tab2 purge;
create table tab2(col1 char(1));
insert into tab2 values('a');

drop table tab3 purge;
create table tab3(col1 char(1), col2 char(1), col3 timestamp);
insert into tab3 values('a','b',systimestamp);
commit;

drop type clobArray;
create type clobArray as table of clob;
/

create or replace function print_row(inTab in varchar2)
  return clobArray
  PIPELINED
  as
	mySQL varchar2(32000);
	myCur sys_refcursor;
	myCLOB clob;
  begin

	mySQL := 'select value(q).getclobval() bob from ' || 
		'( ' || 
		'select extract(node,''/*'') myColNodes ' || 
		'from ( ' || 
			'SELECT VALUE(p) node ' || 
			'FROM (select xmltype(dbms_xmlgen.getxml(''select * from ' || inTab || ''')) myXML from dual), ' || 
			'TABLE(XMLSEQUENCE(EXTRACT(myXML, ''/ROWSET/*''))) p ' || 
		') ' || 
		'), TABLE(XMLSEQUENCE(EXTRACT(myColNodes,''/*''))) q ';


	open myCur for mySql;
	loop
    		fetch myCur into myCLOB;
    		exit when myCur%notfound;
         	pipe row(myCLOB);
     	end loop;
	close myCur;
     return;
  end;
/

show errors


select * from table(print_row('TAB1'));
select * from table(print_row('TAB2'));
select * from table(print_row('TAB3'));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nirvanastorageAuthor Commented:
compiliation returns error: identifier 'CLOBARRAY' must be declared
0
slightwv (䄆 Netminder) Commented:
I ran that entire script as-is using sqlplus.

Did you see/run:
drop type clobArray;
create type clobArray as table of clob;
/

0
nirvanastorageAuthor Commented:
this seems to work when I run the
select * from table(print_row('TAB1'));

from sqlplus. But what if I needed to run the print_row from another PL/SQL procedure? How do I call it from there an print the output to the screen?
0
slightwv (䄆 Netminder) Commented:
We're no back to dbms_outut.put_line.  The difference now is the function now outputs a single clob for each row.

In pl/sql something like (untested, typed in on mobile):

Begin
For I in ( select * from table(print_row('TAB1')) loop
Dbms_output.put_line(I.column_value);
End loop;
End;
/
0
slightwv (䄆 Netminder) Commented:
You might also tweak the function or remove it all together to fit your needs.

It's possible it doesn't need to be a pipelined function.  If not, you don't need the clobarray type.
0
nirvanastorageAuthor Commented:
that works; great thank you!
0
nirvanastorageAuthor Commented:
very speedy response
0
slightwv (䄆 Netminder) Commented:
Glad to help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.