2

i use this query but it not show the column name

SELECT * FROM employeemaster INTO OUTFILE 'c:/order-1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Aditya Dwivedi
  • 252
  • 5
  • 20
  • possible duplicate of [Include column names in the results of an Oracle SQL query?](http://stackoverflow.com/questions/10409845/include-column-names-in-the-results-of-an-oracle-sql-query) – Luke Peterson Jun 28 '14 at 07:19
  • possible duplicate of [Include headers when using SELECT INTO OUTFILE?](http://stackoverflow.com/questions/5941809/include-headers-when-using-select-into-outfile) – Sven R. Jun 28 '14 at 07:20

2 Answers2

3

Use the below query:

SELECT empid as employeeid,sname as staffname FROM employeemaster INTO
OUTFILE 'c:/order.csv' FIELDS 
TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Sandeep
  • 1,504
  • 7
  • 22
  • 32
1

There are two possible solutions:

The simple one : hardcode the colum names yourself and join the header line with the query result :

SELECT 'Name_I_Want_For_Col1', 'Name_I_Want_For_Col1', 'Name_I_Want_For_Col1'
UNION ALL
SELECT ColName1, ColName2, ColName3
    FROM YourTable
    INTO OUTFILE '/path/outfile'

A more straight forward (but more complex one) : Get the colum names from information schema :

select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'"))
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'my_table'
AND TABLE_SCHEMA = 'my_schema'
order BY ORDINAL_POSITION
UNION ALL
    SELECT ColName1, ColName2, ColName3
        FROM YourTable
        INTO OUTFILE '/path/outfile'
Jerry
  • 61
  • 4