简体   繁体   中英

Execute stored procedure DBI Perl

I need to use a stored procedure to gather some data from a database and for that, I've been provided with a sql code:

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

I've replaced sensitive data with XXXXXXXXXXXX. This query works when used in Microsoft SQL Management Studio and it fetches everything that I need to.

My issue is that when I execute this same code on my perl script, it returns a hash reference, what leads me to think that either the I'm not using the sql statement properly or that I'm not being able to "read" the info back.

I'm using this for setting up the sql statement:

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);
}

and this for calling to the database.

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();
    };
}

Also, I've tried to use fetchrow_hashref() instead of fetchrow_array(), but that makes no difference at all.

Hope you can point me on the right direction. Thanks.

EDIT: I've runned the query on MS SQL and it returns 3 results set. Result set 1 is what I need, but don't know how to get to it.

I managed to be able to iterate and gather all the data returned from the stored procedure using this code:

$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);

being $sql a variable that holds the procedure itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM