【问题标题】:Execute stored procedure DBI Perl执行存储过程 DBI Perl
【发布时间】:2019-02-01 22:52:59
【问题描述】:

我需要使用存储过程从数据库中收集一些数据,为此,我得到了一个 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 是我需要的,但不知道如何获得它。

【问题讨论】:

  • 这是一个 Microsoft SQL Server 数据库吗?
  • 是Microsoft SQL,但不确定是不是Server。
  • 不要使用&amp; 调用子例程,并且如果它们实际上带有参数(sub dbMasterSub()),也不要使用空原型声明它们。
  • 所以,正确的做法是调用像 dbMasterSub(param) 这样的子程序并将它们声明为 sub dbMasterSub {,对吧?
  • @LuisDiaz 是的,没错。

标签: sql-server perl dbi


【解决方案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 一个保存过程本身的变量。

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 2013-05-14
    • 2018-03-12
    • 2016-10-31
    • 2013-10-27
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多