Bone_Scavenger has asked for the wisdom of the Perl Monks concerning the following question:

FYI - I'm both a DBD and Perl newbie. I'm trying to figure out how to capture the output from a system stored procedure (specifically, sp_dbcc_recommendations). I thought I could just process the results like those from a select (ie: fetchrow_array()), but no luck. Since the ouput from the proc doesn't appear on my screen when I run the proc using DBI, I'm not sure how to get it. Any help would be appreciated.
  • Comment on DBD:Sybase (how to get output from sp_* procs)

Replies are listed 'Best First'.
Re: DBD:Sybase (how to get output from sp_* procs)
by VSarkiss (Monsignor) on Oct 13, 2004 at 19:36 UTC

    Read the DBD::Sybase docs carefully. Sybase can return multiple result sets from a query, and most of the system stored procedures do in fact do that. You should structure your result retrieval something like this:

    do { while(my $row = $sth->fetchrow_array) { # do something with the row } } while($sth->{syb_more_results});
    Note that you need to check for syb_more_results after your first call to fetch*.

    Update
    I forgot to add: some of what dbcc prints isn't even a result set, it's a server message (the output of print). You'll need to install an error handler, as outlined in the docs under "Sybase-specific attributes". There's an example there of how to print showplan output that you can use as a model.

Re: DBD:Sybase (how to get output from sp_* procs)
by mpeppler (Vicar) on Oct 14, 2004 at 05:55 UTC
    VSarkiss is absolutely right - a lot of output from system stored procedures is sent to the client as "PRINT" messages. These need to be processed in a DBD::Sybase error handler where they will appear with an error code of 0. See the syb_err_handler attribute in the DBD::Sybase documentation for more details.

    Michael

      Thanks for the answers. It has me on the right track, but I'm still having some growing pains. I created a new perl script that implements the "showplan" example from DBI::Sybase document. The problem is that I don't seem to get anything in $msg within the error handler. I get values in $err, $sev, $state, $line, $sql & $err_type. $server and $proc are undefined. $msg is blank. I experienced the same behaviour in a "dbcc checkdb" script I was working on earlier. I could post the "showplan" code if needed, but I'm pretty sure is functionally equivalent to the example. Thanks in advance. I hope it's not a RTFM problem. ;-)
        Hmmm - I tried this:
        #!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect('dbi:Sybase:host=somehost;port=4100', 'sa', '', { syb_err_handler => sub { print "@_\n"; } }); $dbh->do("sp_dbcc_recommendations testdb");
        and got
        0 10 1 237 gndb2 sp_dbcc_run_recommendations The checkstorage run corr +esponding to dbid '4', opid '2', reported no faults; therefore, no co +rrective action is necessary. sp_dbcc_recommendations smlive server The checkstorage run corresponding to dbid '4', opid '2', reported no +faults; therefore, no corrective action is necessary.
        Admittedly this is on a 12.5.0.3 server, so you may get different output.

        Try running with DBI->trace(3) - that should give you all of the outputs from the server in the trace file (stderr by default). You can then try to see what the problem is.

        Michael