问题
I am using the HR Schema and I would like to concatenate all the columns with ',' for every row.
My code is :
SELECT employee_id || ',' || first_name || ',' || last_name || ',' || email || ',' || phone_number || ','|| job_id || ',' || manager_id || ',' || hire_date || ','
|| salary || ',' || commission_pct || ',' || department_id
THE_OUTPUT FROM employees;
Is there a better way (for instance using listagg function) ? I am thinking if the table has 1000 columns I can't do the above
Thanks a lot
回答1:
I would like to concatenate all the columns with ',' for every row
Then use the SQL*Plus commands and generate your desired output. It is all about formatting the output.
- SQL*Plus
For example,
SQL> SET colsep ,
SQL> SET pagesize 20
SQL> SET trimspool ON
SQL> SET linesize 200
SQL> SELECT * FROM hr.employees WHERE ROWNUM <=10;
EMPLOYEE_ID,FIRST_NAME ,LAST_NAME ,EMAIL ,PHONE_NUMBER ,HIRE_DATE,JOB_ID , SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID
-----------,--------------------,-------------------------,-------------------------,--------------------,---------,----------,----------,--------------,----------,-------------
100,Steven ,King ,SKING ,515.123.4567 ,17-JUN-03,AD_PRES , 24000, , , 90
101,Neena ,Kochhar ,NKOCHHAR ,515.123.4568 ,21-SEP-05,AD_VP , 17000, , 100, 90
102,Lex ,De Haan ,LDEHAAN ,515.123.4569 ,13-JAN-01,AD_VP , 17000, , 100, 90
103,Alexander ,Hunold ,AHUNOLD ,590.423.4567 ,03-JAN-06,IT_PROG , 9000, , 102, 60
104,Bruce ,Ernst ,BERNST ,590.423.4568 ,21-MAY-07,IT_PROG , 6000, , 103, 60
105,David ,Austin ,DAUSTIN ,590.423.4569 ,25-JUN-05,IT_PROG , 4800, , 103, 60
106,Valli ,Pataballa ,VPATABAL ,590.423.4560 ,05-FEB-06,IT_PROG , 4800, , 103, 60
107,Diana ,Lorentz ,DLORENTZ ,590.423.5567 ,07-FEB-07,IT_PROG , 4200, , 103, 60
108,Nancy ,Greenberg ,NGREENBE ,515.124.4569 ,17-AUG-02,FI_MGR , 12008, , 101, 100
109,Daniel ,Faviet ,DFAVIET ,515.124.4169 ,16-AUG-02,FI_ACCOUNT, 9000, , 108, 100
10 rows selected.
SQL>
- SQL Developer Version pre 4.1
Alternatively, you could use the new /*csv*/ hint in SQL Developer.
/*csv*/
For example, in my SQL Developer Version 3.2.20.10:
- SQL Developer Version 4.1
New in SQL Developer version 4.1, use the following just like sqlplus command and run as script. No need of the hint in the query.
SET SQLFORMAT csv
来源:https://stackoverflow.com/questions/30902059/oracle-query-concatenate-all-the-columns-with