I'm using
open to have microsoft's
osql.exe execute some sql for me and save it down to file. This works fine except when the file it saves down to is open. ( like in excel for instance. ) At that point, I get an error printed out to console:
Cannot open output file - c:\test\data\extract.csv
No such file or directory
Now I know it's the wrong error getting printed to console, I just want to catch the fact that something bad happenned. how would guys do it?
sub SqlScriptResultsToFile
{
my %args = (
db_srv => '',
db_name => '',
db_user => '',
db_pass => '',
sql => '',
filepath => '',
separator => ',',
columnwidth => 20000,
@_
);
my @errors;
#
# arg validation ...
#
my $osql = "|c:\\Progra~1\\MSSQL7\\Binn\\osql.exe -n -U$args{db_us
+er} -P$args{db_pass} -S$args{db_srv} -d$args{db_name} -o$args{filepat
+h}";
$osql .= " -s$args{separator}" if ( $args{separator} ne '
+' );
$osql .= " -w$args{columnwidth}" if ( $args{columnwidth} ne ''
+);
if ( ! open(PROGRAM,$osql) )
{
push @errors, "invalid filepath [$args{$filepath}]";
return ( success => 0, errors => \@errors );
}
print PROGRAM $args{sql};
close PROGRAM;
return ( success => 1, errors => \@errors );
}