将SQL查询输出写入文本文件/日志文件

时间:2015-03-11 20:41:51

标签: sql sql-server perl

如何将SQL查询的输出写入.txt文件。 这是我想要做的。 SQL查询是在perl脚本中编写的。

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";

这是我得到的错误 - >    无法执行语句:

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'

1 个答案:

答案 0 :(得分:1)

您可以使用bcp实用程序。 如果您可以从Perl运行bat文件,则创建bat文件:

bcp "select TOP 100 book_price from  DatabaseName.SchemaName.bv where book_yield is null" queryout logs.txt -c -T