How to write the output of SQL query to .txt file. Here is what I am trying to do. The SQL query is written inside a perl script.
use strict;
use DataObject;
# create new object, number of default values are set.
my $obj = new DataObject();
# set database server to connect
$obj->SetDBIdent('someDB');
# get fully qualified name
my $bv = $obj->GetTbl("bv");
# query string
my $sql = qq[
select TOP 100 bv.book_price from $bv bv
where bv.book_yield is null
OUTPUT INTO '\\logs.txt'
];
# Execute the query
my $resultSet = $obj->DoSql($sql);
# print the results
foreach my $row (@$resultSet) {
print "$row->{book_price}\t";
Here is the error I get--> Can't execute statement:
select TOP 100 bv.book_price from acctdb.dbo.bv bv
where bv.book_yield is null
OUTPUT INTO '\logs.txt'
Transaction Rolled Back
DBD::Sybase::st execute failed: Server message number=156 severity=15 state=2 line=4 server=SYBDEVtext=Incorrect syntax near the keyword 'OUTPUT'.
Statement=
select TOP 100 bv.book_price from bv
where bv.book_yield is null
OUTPUT INTO '\logs.txt'
OUTPUT INTO '\\logs.txt'is not valid in a SQL statement. You would have to retrieve the data into your program and then output it from there.OUTPUT INTOis valid T-SQL (although all of the examples I saw include a list of columns, i.e.OUTPUT foo INTO ...).OUTPUT INTO '\\logs.txt'from your query, open a filehandle, and change yourprintstatements to write to the handle instead ofSTDOUT, e.g.:my $file = 'logs.txt'; open my $fh, '>', $file or die "Failed to open '$file': $!";andforeach my $row (@$resultSet) { print $fh "$row->{book_price}\t"; }