How to add column names to the csv file created by SQL output statement?

user1414843 picture user1414843 · May 24, 2012 · Viewed 7.4k times · Source

I used this statement

SELECT * FROM SalesOrders; 
OUTPUT TO 'e:\\output\\rule_criteria.csv' WITH COLUMN NAMES;

I get an error saying 'WITH' was not expected here. Any way to resolve this? I'm using SQL Anywhere 10.

Answer

Tyson Lewis picture Tyson Lewis · Jul 11, 2012

WITH COLUMN NAMES was an extension that was added to SQL Anywhere beginning in version 12.0.0, so SQL Anywhere 10.x won't be able to accomplish this task.

You could however output the column names to the file first, and then append the results of the select statement:

I.e.

SELECT LIST("name" ORDER BY column_number ASC) 
FROM sa_describe_query('SELECT * FROM SalesOrders');
OUTPUT TO 'file.csv';

SELECT * FROM SalesOrders;
OUTPUT TO 'file.csv' APPEND;

The full documentation for OUTPUT TO in version 10 is here:

OUTPUT statement [Interactive SQL]