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

Hi

I am new to MSSQL and PERL (having always used Sybase or Oracle). I have noticed that when I execute a stored proc and raise a user defined error ..

CREATE PROC SP_DAVE AS BEGIN update mytable set st = getdate() where date = getdate(); if @@rowcount != 1 begin RAISERROR ('No Rows !!',16,1); RETURN -1; end RETURN 0; END;
Now ... my perl will not pick up this user defined error !! It sees the first command executed succesfully then ignores subsequent errors.

a brief summary of my perl code..
$sql = "EXEC SP_DAVE"; $sth = $dbh->prepare($sql); $sth->execute or print "\nError EXECUTE\n$DBI::errstr\n";
...

This will not report the user defined error !!

Edit by castaway - added code tags

Replies are listed 'Best First'.
Re: Trapping Stroed Proc errors with MSSQL
by gellyfish (Monsignor) on Jul 22, 2004 at 11:03 UTC

    Have got RaiseError => 1 in your DBI->connect()? You also might want to try this with an error severity of greater than 16 (i.e. a system error) for experimental purpose to see what happens.

    /J\

      Thanks for quick reply, but RaiseError does not seem to pick these up either (I have already tried experimenting with the severity with no luck either)
      Dave
Re: Trapping Stroed Proc errors with MSSQL
by mpeppler (Vicar) on Jul 22, 2004 at 17:48 UTC
    First - which DBD module are you using?

    Second - try running this with DBI->trace(3) to see what the server sends back, and how your DBD driver interprets this.

    Michael

Re: Trapping Stroed Proc errors with MSSQL
by EdwardG (Vicar) on Jul 23, 2004 at 08:02 UTC

    You need to tell SQL Server to NOT return a "x rows affected" message, using SET NOCOUNT ON.

    ALTER PROC SP_DAVE AS BEGIN SET NOCOUNT ON -- << ADD THIS LINE update mytable set st = getdate() where date = getdate(); if @@rowcount != 0 begin RAISERROR ('No Rows !!',16,1); RETURN -1; end RETURN 0; END;

     

      Thanks

      This is it !! Perl was reading thru all the system messages and hence missing the error message if it was other than first.

      Thanks for everyones help