English 中文(简体)
How to output data from iSQL to csv file _with_ headings?
原标题:

I m trying to query a Sybase ASA 8 database with the iSQL client and export the query results to a text file in CSV format. However the column headings are not exported to the file. There is no special option to specify that, neither in the iSQL settings nor in the OUTPUT statement.

The query and output statement looks like this:

SELECT * FROM SomeTable;
OUTPUT TO  C:	empsometable.csv  FORMAT ASCII DELIMITED BY  ;  QUOTE   

The result is a file like

1;Miller;Steve;1980-06-28
2;Jones;Martha;1965-11-02
3;Waters;Richard;1979-10-15

while I d like to have

ID;LASTNAME;FIRSTNAME;DOB
1;Miller;Steve;1980-06-28
2;Jones;Martha;1965-11-02
3;Waters;Richard;1979-10-15

Any hints?

最佳回答

I would have suggested to start with another statement:

SELECT  ID;LASTNAME;FIRSTNAME;DOB  FROM dummy;
OUTPUT TO  C:\temp\sometable.csv  FORMAT ASCII DELIMITED BY  ;  QUOTE   ;

and add the APPEND option on your query... but I can t get APPEND to work (but I m using a ASA 11 engine).

问题回答

Try this one

SELECT  ID , LASTNAME , FIRSTNAME , DOB  union
SELECT string(ID),LASTNAME,FIRSTNAME,DOB FROM SomeTable;
OUTPUT TO  C:\temp\sometable.csv  FORMAT ASCII DELIMITED BY  ;  QUOTE   ;

Simply add the option

WITH COLUMN NAMES

to your statement and it adds a header line with the column names.

The complete statement is therefore:

SELECT * FROM SomeTable; OUTPUT TO  C:	empsometable.csv  FORMAT ASCII DELIMITED BY  ;  QUOTE    WITH COLUMN NAMES

See sybase documentation.

I am able to use the isql command to output quoted CSV.

Example

$ isql $DATABASE $USERNAME $PASSWORD -b -d, -q -c
select username, fullname from users

gives the result:

username,fullname
"jdoe","Jane Doe"
"msmith","Mark Smith"

Command-line flags

(copied from the man page)

-b: Run isql in non-interactive batch mode. In this mode, the isql processes its standard input, expecting one SQL command per line.

-dDELIMITER: Delimits columns with delimiter.

-c: Output the names of the columns on the first row. Has any effect only with the -d or -x options.

-q: Wrap the character fields in double quotes.

Escaping Issue

You might run into problems if the query results contain double-quotes, though. The quotes aren t escaped properly, so they result in invalid CSV:

> select  string","with"quotes  as quoted_string
quoted_string
"string","with"quotes"

You are already familiar with the OUTPUT options. There is no option that gives you what you want.

Ok, the problem is the receiving end does not accept standard CSV files, it needs semi-colons.

If you are scripting, then you are better off getting the output in the format that is closest to what you need, and then awk-ing the output file. Very fast and you can change anything you need. I think your best option is ASCII or default output format, which will provide Comma (not colon) Separated Values, in an ASCII character text file, and includes column Headers. Then use a single awk command to convert the commas to semi-colons.

Found an easier solution, Place the headers in one file say header.txt ( it will contain a single line "col_1|col_2|col_3") then to combine the header file and your output file run:

cat header.txt my_table.txt > my_table_wth_head.txt
isql -S<Server> -D<Database>-U<UserName> -s ; -P<password>$1 -w 10000 -iname.sql > output.csv

If you use the FORMAT EXCEL option, it will output the rows with the column name in the first row. Then once you get it into excel you can save it into another format if you need to.

SELECT * FROM SOMETABLE;
OUTPUT TO  C:	empsometable.xls  FORMAT EXCEL DELIMITED BY  ;  QUOTE   

Recently I needed to solve similar issue with some prehistoric ASA7 which does not support the WITH COLUMN NAMES for .CSV output.

The solution for me was the .DBF file, which has the columns structure in it and can be processed automatically, much better than .XLS

SELECT * FROM SomeTable;
OUTPUT TO  C:	empsometable.dbf  FORMAT DBASEIII;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签