in reply to Saving SQL PRINT statements - MSSQL, SQL Server, DBD::ODBC.

I've done this...and the code is somewhere (it'll take a while to dig up)...but basically you have to look into odbc_err_handler in the DBD::ODBC docs, the print statements are caught there as errors.

And I see that the docs mention some examples under the odbc_err_handler section. So maybe I don't have to dig up that code :-)

  • Comment on Re: Saving SQL PRINT statements - MSSQL, SQL Server, DBD::ODBC.

Replies are listed 'Best First'.
Re^2: Saving SQL PRINT statements - MSSQL, SQL Server, DBD::ODBC.
by 18th_bronzeman (Novice) on Sep 22, 2007 at 16:58 UTC
    Thank you for the response!

    I have actually used the code in the DBD::ODBC docs. That is what you see modified above. I wrapped all of the error trapping code in a subroutine. That was not included. The while on the end handles the PRINT statements if/when the occur.

    I can print to console from within that subroutine. I can make that subroutine return its output as a variable. What I don't understand is how to take that variable and place it into the same data structure as the sql results in the order it returned from the database.

    Thanks again for the response.

      sorry, misunderstood the question due to lack of formatting. Wrap your code in <code></code> tags. Your error handler should be in the same scope as your while loop. A rough outline is:
      { my @output; local $dbh->{odbc_err_handler} = sub { .... push @output, $print_statement; ... }; while ($sth->{odbc_more_results}) { while (my $data = $sth->fetchrow_array()) { ... push @output, (join("|", @$data) . "\n"); } } }