Thursday, May 20, 2010

Dynamic concatenation of columns in oracle for a given Table

We can use the following anonymous block to concatenate all the columns in a given table.

The below anonymous block takes tablename as a parameter and gives all column names separated by comma.


declare
v_RETURN_STRING varchar2(200);
begin
for rec in (select column_name
from user_tab_columns
where table_name = 'TABLE_NAME')
loop
if v_RETURN_STRING is null then
v_RETURN_STRING := rec.column_name;
else
v_RETURN_STRING := v_RETURN_STRING ||','|| rec.column_name;
end if;
end loop;
dbms_output.put_line(v_RETURN_STRING);
end;

No comments: