![](/img/trans.png)
[英]Issue with binding parameters for MS SQL Server Stored Procedure in Perl DBI
[英]Execute stored procedure DBI Perl
我需要使用存儲過程從數據庫中收集一些數據,為此,我提供了一個sql代碼:
declare @p3 xml
set @p3=convert(xml,N'<root><r i="XXXXXXXXXXXX"/></root>')
declare @p8 xml
set @p8=convert(xml,N'<root><r i="274"/><r i="276"/><r i="275"/><r i="155"/><r i="20"/><r i="520"/><r i="758"/><r i="760"/><r i="156"/><r i="172"/></root>')
exec spu_SearchItems @siteName=N'XXXXXXXXXXXX',@searchString=N'*',@searchLocations=@p3,@includeChildren=0,@includeSearchLocations=0,@includeExtensions=0,@maxResults=501,@classIds=@p8
我已用XXXXXXXXXXXX替換了敏感數據。 在Microsoft SQL Management Studio中使用時,此查詢有效,並且可以獲取所需的一切。
我的問題是,當我在perl腳本上執行相同的代碼時,它返回一個哈希引用,這導致我認為我未正確使用sql語句或無法“讀取”回來的信息。
我正在使用它來設置sql語句:
sub newquery {
my $sql = q(declare @p3 xml
set @p3=convert(xml,N'<root><r i="XXXXXXXXXXXX"/></root>')
declare @p8 xml
set @p8=convert(xml,N'<root><r i="274"/><r i="276"/><r i="275"/><r i="155"/><r i="20"/><r i="520"/><r i="758"/><r i="760"/><r i="156"/><r i="172"/></root>')
exec spu_SearchItems @siteName=N'XXXXXXXXXXXX',@searchString=N'*',@searchLocations=@p3,@includeChildren=0,@includeSearchLocations=0,@includeExtensions=0,@maxResults=501,@classIds=@p8);
&dbMasterSub($sql);
}
這用於調用數據庫。
sub dbMasterSub() {
my $sql = shift;
my @row;
# print $sql."\n";
my $port = 1443;
my $dsn = "Provider=sqloledb;Trusted Connection=yes;";
$dsn .= "Server=" . $myServer . ";Database=$myDB;";
eval {
my $dbh = DBI->connect( "dbi:ADO:$dsn", $myUser, $myPass, { RaiseError => 1, AutoCommit => 1 } );
my $sth;
if ( $sql eq "ping" ) {
my $ping = $dbh->ping();
return $ping;
}
else {
$sth = $dbh->prepare( $sql );
#Execute the statement
$sth->execute();
print "sth -> $sth\n";
open( my $fh, '>:encoding(UTF-8)', $tagsPaths );
print $fh "{\n";
my $index = 0;
while ( my $row = $sth->fetchrow_array() ) {
print "$row" . "\n";
if ( $index == 0 ) {
print $fh $row;
$index = $index + 1;
}
else {
print $fh ",\n" . $row;
}
}
print $fh "\n}";
close $fh;
$sth->finish();
}
$dbh->disconnect();
};
}
另外,我嘗試使用fetchrow_hashref()而不是fetchrow_array(),但這一點都沒有區別。
希望你能指出正確的方向。 謝謝。
編輯:我已經在MS SQL上運行查詢,它返回3個結果集。 結果集1是我需要的,但不知道如何實現。
我設法能夠使用以下代碼來迭代和收集從存儲過程返回的所有數據:
$sth = $dbh->prepare($sql);
$sth->execute();
my $more_results;
my $count = 0;
my $tinyIndex = 0;
do{
$count++;
print "\n\tdataset $count\n";
my $names = $sth ->{NAME};
print join(";",@$names),"\n";
while (my @row = $sth->fetchrow_array()){
print join(";",@row),"\n";
$tinyIndex++;
}
if ($@) {
print "FAILED\n$@";
}
print "$tinyIndex rows\n";
}while($more_results = $sth->more_results);
是$ sql一個變量,它保存過程本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.