Inserting tabs in SQL output [message #510874] |
Wed, 08 June 2011 03:07  |
 |
fouture
Messages: 7 Registered: June 2011 Location: Leuven, Belgium
|
Junior Member |
|
|
Hi,
I'm looking for a way to insert tabs (ASCII 009) in SQL output, to make the output file Excel-ready without manual intervention.
Here's what I need :
SQL> set output myfile.xls;
SQL> select article,TAB,count(*) from sales group by article having count(*) > 0;
Instead of TAB I can insert a dollar sign, but then I have to replace it using a file editor, which is a manual operation.
I have been looking for an OpenVMS application which can replace all dollar signs by tabs in a given file, with no luck.
I created a DCL procedure (simplified from GSR.COM which I found on the internet) that reads every record in the output file and substitutes dollar signs by tabs, but it doesn't work with tabs, only with "normal" characters...
Even if I use the OpenVMS editor to insert ASCII 009 tab characters in the SQL statement (using the GOLD 009 GOLD SPEC INS key combination), SQL apparently automatically substitutes tabs by full stop signs in the output :
select article,' ',date from werknemers limit to 2 rows;
ARTICLE DATE
TROUSERS . 12-28-1974
SHIRTS . 10-08-1973
2 rows selected
I can use SQL to create a CSV file (comma separated values) which can be opened by Excel :
"trousers",20
"shirts",30
But the user has to open this file from within Excel and make a number of choices (separator, text delimiter, header row) before (s)he gets to see the desired columnar spreadsheet. That's not automation : I need the generated SQL output file to be double-clickable so that it opens as a columnar spreadsheet immediately.
There must be a simple solution, but after two days of googling for it I still have no workable solution.
I searched this forum, and came up with zilch.
My inspiration has come to an end. So: help !?
Bart
|
|
|
Re: Inserting tabs in SQL output [message #510875 is a reply to message #510874] |
Wed, 08 June 2011 03:10   |
cookiemonster
Messages: 13972 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
SQL> select rownum||chr(9)||rownum from dual connect by level < 10;
ROWNUM||CHR(9)||ROWNUM
--------------------------------------------------------------------------------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
9 rows selected.
SQL>
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Inserting tabs in SQL output [message #510897 is a reply to message #510895] |
Wed, 08 June 2011 03:58   |
 |
fouture
Messages: 7 Registered: June 2011 Location: Leuven, Belgium
|
Junior Member |
|
|
Roachcoach wrote on Wed, 08 June 2011 10:54Sorry, I thought it was Oracle 7 noted in a funny manner, that's really old 
try
select dump(' ') from dual
See what it comes back with.
You'd be surprised how many newspapers would be printed, or cars assembled *without* Oracle Rdb... I guess none, at least in Belgium.
But it doesn't work :
%SQL-F-RTNNOTDEF, function or procedure DUMP is not defined
I guess I'm looking for a standard ANSI SQL solution here.
[Updated on: Wed, 08 June 2011 04:00] Report message to a moderator
|
|
|
|
|
Re: Inserting tabs in SQL output [message #510900 is a reply to message #510898] |
Wed, 08 June 2011 04:17   |
 |
fouture
Messages: 7 Registered: June 2011 Location: Leuven, Belgium
|
Junior Member |
|
|
Littlefoot wrote on Wed, 08 June 2011 11:07A.CSV file would have its columns separated by semi-colon (choose any other character (automatically) recognized by Excel). If you double-click that A.CSV, Excel would open it and display every (file) column in its (spreadsheet) column.
Indeed, it does
As I thought : there is a simple solution !
I just have to use semicolons as separators, doublequote all text variables, and output to this SQL command :
SQL> set output myfile.csv;
Doubleclicking the file automatically launched OpenOffice, but I quickly changed that to Excel for all .csv files.
A lot of thanks !
If anyone has questions about Oracle Rdb, ModularSQL and the like on OpenVMS, I'll return the favour !
Bart
|
|
|
Re: Inserting tabs in SQL output [message #510915 is a reply to message #510900] |
Wed, 08 June 2011 06:02   |
 |
Littlefoot
Messages: 21826 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
When we're still at it, as you mentioned OpenVMS in the initial message, here's how you could replace a dollar sign using a DCL script. I know that DCL isn't what we talk about here, on OraFAQ Forum, but I simply couldn't resist.
I tried to replace it with a <Tab> (Ctrl+I in a DCL script) - it was successful, at least the output file suggested so, but - after it was FTPed to a PC, Excel ignored it and put the whole string into a single column. That's why I replaced a dollar sign with a semi-colon.
Took a while, I forgot most of it (what a shame!):
Input file (TEST_IN.TXT)
10$Little$Foot
20$Michel$Cadot
30$Cookie$Monster
Code:
$ open/read/error=end bk test_in.txt
$ open/write/error=end wt test_out.csv
$
$ rep_char = ";"
$
$ loop_records:
$ read/end_of_file=end bk zapis
$
$ new_zapis = ""
$ ostatak = zapis
$ l_length = f$length(zapis)
$
$ loop_in_record:
$
$ dolpos= f$locate("$", ostatak)
$ if f$length(new_zapis) .gt. l_length then goto end_1
$
$ dio = f$extract(0, dolpos, ostatak)
$ ostatak = f$extract(dolpos + 1, l_length, ostatak)
$ new_zapis = new_zapis + dio + rep_char
$ goto loop_in_record
$
$ end_1:
$ new_zapis = f$extract(0, f$length(new_zapis) - 1, new_zapis)
$ write wt new_zapis
$ goto loop_records
$
$ end:
$ close bk
$ close wt
A script reads the input file, record by record. Searches for the dollar sign (DOLPOS = $ position) and creates a new string (NEW_ZAPIS), piece by piece (within LOOP_IN_RECORD loop).
For example, "10$Little$Foot" is converted to1st iteration: 10;
2nd iteration: 10;Little;
3rd iteration: 10;Little;Foot; A terminating ; is removed before the new string is written into the output (TEST_OUT.CSV) file which, finally, looks like
10;Little;Foot
20;Michel;Cadot
30;Cookie;Monster
|
|
|
|
|
Re: Inserting tabs in SQL output [message #511063 is a reply to message #510915] |
Thu, 09 June 2011 06:20  |
 |
fouture
Messages: 7 Registered: June 2011 Location: Leuven, Belgium
|
Junior Member |
|
|
Thanks, LittleFoot:
Your DCL procedure works perfectly, and also with tabs !
I can now simply insert dollar signs in the SQL output between the columns - we only use euros over here anyway - and when finished, run this procedure to replace them with tabs.
A direct tab-delimited output would have been ideal, but this is very acceptable, thank you very much.
To the other contributors : these solutions (set termout, set colsep) do not work with Oracle Rdb, or would require the installation and mastering of additional software (only as a last resort), so thanks a lot for the suggestions but I'll do it with the DCL procedure.
Bart
|
|
|