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

i've got perl using DBI to connect to our MS SQL Server database.. everythings working fine so far.. but i can't figure out how to print out an error message when there's an error in a trigger on a MS SQL table.. here's the main part of the code:
if($dbh = DBI->connect($connectionInfo,$userid,$passwd)){ $sth = $dbh->prepare($query); if($sth->execute()){ @dsetcols=[]; $xi=0; @fieldnames = split(/,/, $fnames); foreach $fieldname (@fieldnames) { $ds{$dsetname}{$fieldname}=[]; push @dsetcols,""; $sth->bind_col($xi+1,\$dsetcols[$xi]); if ($fieldname eq "task_id") { } $xi++; } while($sth->fetch()) { $xi=0; foreach $fieldname (@fieldnames) { push @{$ds{$dsetname}{$fieldname}},safedecode($dsetcol +s[$xi]); $xi++; } } $sth->finish(); }else{ print "Query \""; print $query; print "\" failed.<br>"; print "Database error message: "; print $dbh->errstr; print "<p>"; # used in do_autorefresh, detail window won't automatica +lly close if query failed $badquery = 1; } $dbh->disconnect; }else{print "Could not connect to database.<br>";} } $x++; }
when there's an error in a query, like say its trying to update a field in the table and the type isn't correct, it'll print out my error message.. however, i have a trigger on a table, and in that trigger, if it found something, i.e. field1 != field2, then it will raise an error in SQL server, but it looks like DBI doesn't catch that error here's the code inside the trigger:
IF (@max_vendor != (SELECT vendor_id FROM Inserted)) BEGIN RAISERROR ('PO already exists with a different vendor',16,1) ROLLBACK TRAN END
any idea on why this isn't working? ive been doing some testing.. and DBI will report the SQL error given by the trigger if i'm doing an INSERT statement into a table and that table has a trigger on inserts and updates.. however if i do a UPDATe statement on that table, DBI would report the SQL error given by the trigger thanks!

Replies are listed 'Best First'.
Re: perl w/ DBI connectiong to MSSQL, not reporting SQL server trigger errors
by runrig (Abbot) on Jan 06, 2005 at 19:01 UTC
    Set RaiseError to true on the connect, wrap the whole thing in an eval block, and check $@ after the block. I'd bet it is failing where you are not checking for failure. RaiseError will check all DBI calls for you :-)

    Update: and use strict (super search here for tutorials and meditations).

      For debugging purposes, it's almost better not to wrap it in eval. $dbh->{RaiseError}=1 will cause each DBI method to die with the generated error message, which by default will print to STDERR.

      Anima Legato
      .oO all things connect through the motion of the mind

        You are correct, depending on whether or not you want the rest of the script to continue. But rather than setting $dbh->{RaiseError} = 1 I usually prefer adding a {RaiseError => 1} argument to the connect, so that even connect errors are caught by RaiseError.