in reply to MS SQL Stored Procedure Syntax

Platform: WinNT

Connector: DBI::ODBC

Here's my revised code:

#!/usr/bin/perl -w use strict; use DBI; #Database handle object my $dbh; my $user='sa'; my $password=''; #Statement handle object my $sth; #General return code my $rc; #Connect to the SQL database CQmaster $dbh = DBI->connect("dbi:ODBC:CQmaster",$user,$password) || die "Can't + open! $!"; #Prepare the query and execute it $sth= $dbh->prepare("SELECT * FROM sysdatabases"); $sth->execute(); while (my @row = $sth->fetchrow_array) { print "@row\n"; } #Call stored procedure sp_spaceused $sth=$dbh->prepare('EXEC sp_spaceused'); $sth->execute(); while (my @row = $sth->fetchrow_array) { print "@row\n"; } $rc = $dbh->disconnect;
And I get the following error message:

DBI::db=HASH(0x1c9e4f4)->disconnect invalidates 1 active statement handle (either destroy statement handles or call finish on them before disconnecting) at C:\scripts\FIRSTODBC.PL line 33.

How can I extract the output of the stored procedure sp_spaceused without resulting in this error message?

Replies are listed 'Best First'.
Re: Re: MS SQL Stored Procedure Syntax
by chromatic (Archbishop) on Sep 27, 2001 at 23:59 UTC
    DBI should clean things up for you when you read the end of the result set, but the fix is probably to call finish() manually.
    $sth->finish(); $rc = $dbh->disconnect();
    It's just a warning that the statement handle is still marked as active. That could mean you have more results to read, or that the server's allocated some resource and hasn't give it back yet. You could disable warnings, but it's probably better just to call finish.

    ©

Re: Re: MS SQL Stored Procedure Syntax
by runrig (Abbot) on Sep 28, 2001 at 00:03 UTC
    Doubt if this has anything to do with your problem, but I see a couple of other problems.
    This is wrong:
    $dbh = DBI->connect("dbi:ODBC:CQmaster",$user,$password) || die "Can't + open! $!";
    First, $! does not return error messages from DBI, you want $DBI::errstr instead (or RaiseError, see the next note).
    Second, you're not checking the status of the prepare and execute statements. You could either check each statement, or use RaiseError on the connect (see the DBI docs).

    I doubt that'll magically fix anything, so after that, if the warning still occurs, you can try putting $sth->finish after one or both of the fetch loops, though it seems like it should be unnecessary (maybe it gets confused with a stored procedure?).