|
|
Re: Display the columns of a table into rows delimited by comma [message #651477 is a reply to message #651467] |
Wed, 18 May 2016 10:53 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Is this what you want:
SQL> select listagg(column_name,',') within group (order by column_id) cols
2 from user_tab_columns where table_name='&table_name';
Enter value for table_name: EMP
COLS
----------------------------------------------------------------------------
EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO
1 row selected.
SQL> /
Enter value for table_name: DEPT
COLS
----------------------------------------------------------------------------
DEPTNO,DNAME,LOC
1 row selected.
|
|
|
|
Re: Display the columns of a table into rows delimited by comma [message #651483 is a reply to message #651481] |
Thu, 19 May 2016 00:05 |
|
Littlefoot
Messages: 21808 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Something like this?SQL> set colsep ,
SQL> select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp;
EMPNO,ENAME ,JOB , MGR,HIREDATE, SAL, COMM, DEPTNO
----------,----------,---------,----------,--------,----------,----------,----------
7369,SMITH ,CLERK , 7902,17.12.80, 1600, , 20
7499,ALLEN ,SALESMAN , 7698,20.02.81, 2400, 300, 30
7521,WARD ,SALESMAN , 7698,22.02.81, 2050, 500, 30
|
|
|
|
Re: Display the columns of a table into rows delimited by comma [message #651501 is a reply to message #651485] |
Thu, 19 May 2016 04:16 |
grpatwari
Messages: 288 Registered: June 2008 Location: Hyderabad
|
Senior Member |
|
|
Thank you cadot and Littlefoot.
I have tried and working now.As of now it is fine. I will check with my requirement if any change.
select 'SELECT ' || listagg(column_name,',') within group(order by column_id) || ' FROM ' || '&tablename;' cols
from user_tab_columns where table_name='&table_name';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Display the columns of a table into rows delimited by comma [message #651590 is a reply to message #651578] |
Fri, 20 May 2016 09:33 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
grpatwari wrote on Fri, 20 May 2016 12:47Generally table will be passed from application.Need to generate the query and execute the query based on the provided table name.This is the exact requirement.
Shall the result set be treated anyhow? I see no remark about in that requirement nothing about it, so I doubt what is the purpose behind it. Just arbitrarily run some queries to make DB process random data?
(you already were asked by Ed where should that output go, but I do not see any attempt to answer this question)
|
|
|
Re: Display the columns of a table into rows delimited by comma [message #651592 is a reply to message #651577] |
Fri, 20 May 2016 09:54 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Kumar876 wrote on Fri, 20 May 2016 12:15...
Please note that - there is possibility of getting the below error while using LISTAGG when list being created is too long
ORA-01489: result of string concatenation is too long error
...
The result of LISTAGG is limited to 4000 bytes. If you have many columns in a table the result could exceed this value.
|
|
|