dbTalk Databases Forums  

Cycle thru columns in table?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Cycle thru columns in table? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rich
 
Posts: n/a

Default Cycle thru columns in table? - 03-02-2010 , 04:09 PM






1 am using 10g.

I need code that returns the column name of the table and the value of
a row of code. For example, assume I have a table called Customer.
Then imagine if there is a column called First_Name. My SQL will
return one record. So that the output might look like

First_Name = Fred

The psudo code might look like

For i = 1 to the last column in table
column_name := column(i)
record_value := cursor(i)
Loop

How can I do the above in Oracle?

Reply With Quote
  #2  
Old   
Kay Kanekowski
 
Posts: n/a

Default Re: Cycle thru columns in table? - 03-09-2010 , 09:35 AM






Hi,
try this:

create table kk_cdom
( first_name varchar2(30),
last_name varchar2(30),
birthday date,
children number);

insert into kk_cdom values ( 'fred', 'flintstone', sysdate-30000,
2);
insert into kk_cdom values ( 'barnie','gröllheimer', sysdate-29900,
3);

select * from kk_cdom;
declare
cursor col is
select COLUMN_NAME
from user_tab_columns
where table_name = 'KK_CDOM'
order by COLUMN_id;
inhalt varchar2(30);
--
begin
for spalte in col loop
execute immediate ( 'select to_char(' || spalte.column_name || ')
from KK_CDOM where rownum < 2' ) into inhalt;
dbms_output.put_line ( spalte.column_name || ' : ' || inhalt);
end loop;
end;
/

And that is the result:

FIRST_NAME : fred
LAST_NAME : flintstone
BIRTHDAY : 19.01.28
CHILDREN : 2

hth
Kay

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.