PEPPLER.ORG
Michael Peppler
Sybase Consulting
Menu
Home
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
Freeware
Sybperl
Sybase::Simple
DBD::Sybase
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
FAQs
Sybase on Linux FAQ
Sybperl FAQ
Personal
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: Michael Peppler <mpeppler at peppler dot org>
Subject: Re: How to get SP error messages
Date: Jul 23 2002 2:11PM

On Mon, 2002-07-22 at 17:53, Alex Quezada wrote:
> Hi,
> 
> 	I'm using Sybase::DBlib to execute an SP, and this SP returns an error message:
> 
> couser-SF_CON_DEV1 1>EditElement @elementId=953684, @userId=100;
> Msg 20023, Level 16, State 1
> Server 'SF_CON_DEV1', Procedure 'EditElement', Line 76
> Alex Quezada already has that 953684 checked out.
> (return status = -114)
> 
> So I'd like to pass this error message along, but for the life of me I can't find the method to read the error message.  I can grab the return status just fine, but that isn't enough since it doesn't give me the error details (like who has checked stuff out).

See the dbmsghandle() entry in the Sybase::DBlib man page.

The error number and the error message are passed to the handler that
you define with dbmsghandle() - it's up to you to save them someplace
where you can use them again later. 

There are various tricks that you can use to store the error in the $dbh
itself. For example:

use Sybase::DBlib;

sub message_handler
{
    my ($db, $message, $state, $severity, $text, $server, $procedure,
$line)
	= @_;

    # Don't display 'informational' messages:
    if ($severity > 10)
    {
        $db->{LASTERR}->{errno} = $message;
	$db->{LASTERR}->{text} = $text;
    }
    elsif ($message == 0)
    {
	print STDERR ($text, "\n");
    }
    
    0;
}

dbmsghandle(\&message_handler);    # register the message handler

my $dbh = Sybase::DBlib->new($user, $pwd, $server, $appname,
                        { LASTERR => {} });

$dbh->dbcmd(...);
my $ret;
if(($ret = $dbh->dbsqlexec) == SUCCEED) {
    while(($ret = $dbh->dbresults) != NO_MORE_RESULTS) {
        last if $ret == FAIL;
        ... # process results...
    }
}
if($ret == FAIL) {
    print "Got error $dbh->{LASTERR}->{errno}:
$dbh->{LASTERR}->{text}\n");
}



This is just an example - coded off the top of my head (so there may
very well be syntax errors!), but it should give you an idea of how you
can handle saving error messages from the handler.

See also the sybutil.pl file from the sybperl distribution - it includes
a sample message_handler() subroutine.

Michael
-- 
Michael Peppler / mpeppler@peppler.org / http://www.mbay.net/~mpeppler
mpeppler@zetatools.com / ZetaTools, Inc / http://www.zetatools.com
ZetaTools: Call perl functions as Sybase stored procedures!