I have code like this

sub DBCC { my $database = shift(); my $text = ''; my $dbh = DBI->connect('dbi:ODBC:jobodbc2', 'xxxx', 'xxxx', {PrintError => 0,RaiseError => 1,LongReadLen => 65536, odbc_async_exec => 1, odbc_err_handler => sub { my ($state, $msg) = @_; # Strip out all of the driver ID stuff $msg =~ s/^(\[[\w\s]*\])+//; $text .= $msg."\n"; return 0; } }); my $sth = $dbh->prepare("dbcc CHECKDB ('$database')"); $sth->execute; $dbh->disconnect(); return $text; }
to run the DBCC CHECKDB and capture the messages. It works perfect. I'd like to do the same thing for a stored procedure (some daily database maintenance task) that prints some headings and the counts of rows affected when run via the Query Analyzer, but can't seem to be able to do so. If I replace the "dbcc CHECKDB ('$database')" by "EXEC dbo.dbAdm_Daily" I get
DBD::ODBC::st execute failed: (DBD: dbd_describe/SQLNumResultCols err= +-1)

The procedure works when started from QueryAnalyzer and prints the messages. It works as well if called with $dbh->do(), but the messages are not captured in this case. What am I doing wrong?


In reply to DBI+MS SQL, capture prints from stored procedures by Jenda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.