Once upon a time, way back in the mists of time (i.e. sometime in 1990 :-) I started my first version of "sybperl" like this: write the SQL to a temp file, run isql and parse the output. Then I got an email from Larry about using pseudo-TTYs and running isql as a sub-processes - I still have that code somewhere (1990 vintage perl...) if anyone is interested.
Assuming that this is Sybase (maybe MS-SQL has it as well) you should use the "-s" command-line argument to specify a column separator that is unlikely to occur in the data. You should also specify the "-w" argument with a large integer to make sure that isql doesn't wrap any of the output lines. So something like this could work:
open(TMP, ">/tmp/t$$.sql") || die "can't open temp file: $!";
# Set nocount to avoid the "xxx rows affected" message in isql
print TMP "set nocount on\ngo\nselect * from the_table\ngo\n";
close(TMP);
# Note: -b flag suppresses column headers.
open(ISQL, "isql -Ujoe -Pjoespwd -Smyserver -s'%%^^%%' -w 1024 -b -i /
+tmp/t$$.sql") || die "Can't run isql: $!";
while(<ISQL>) {
chomp;
my @row = split(/%%^^%%/, $_);
.... do something with the @row of data....
}
close(ISQL);
That being said - using DBI and the appropriate DBD driver is a lot better. It's faster, and error handling is
much easier.
Michael
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.