Monday, August 09, 2004

For all those who google about this in the future.

It's taken me most of today to figure this out, and I couldn't tell you exactly how I found it, but if this subject is of interest to you, read on. If you have to run SQL statements into a SQL server database, and you're using ADODB and PERL to do it, then here is the syntax on how to acquire the error statement if it doesn't work.

$rs = $conn->Execute("Select * FROM generic.table");
if (! $rs) {
$ErrMsg = $conn->Errors(0)->{Description};
if($ErrMsg ne "") { print
$ErrMsg . "\n"; Die; }
else { print "Query is empty!\n"; }
}


That big red zero is important. Every single example of this I found today didn't have that zero there, and instead wanted me to use the Errors function to return a hash. Well it didn't F***in' work! So, after just about giving up, I've found this solution. BTW the else statement may be unnecessary. If recordset($rs) is undefined, it probably will always be in error. A successful query returning no values probably comes back defined, but with no data($rs->EOF is true).

UPDATE: upon furthur consideration, I think that the algorithm above is inadequate. Here is the new modified algorithm. It eliminates the else, but will probably give the full range of errors. I believe that the zero is an index, and that if there are more errors the value as a parameter for Function Error will increase. So....

if (!$rs) {
if($con->Errors()->Count > 0) {
for($a=0;$a<$con->Errors()->Count;$a++) {
print "Error($a): " . $con->Errors($a)->{'Description'} . "\n"; }
die; }
}

In this example, $a represents the index of each error up to the total count of errors.

0 Comments:

Post a Comment

<< Home