http://www.perlmonks.org?node_id=51466

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

I am using Win32::ODBC and SQL Server 7.
I write an update query to the DB, and I would really like to be able to get back the number of rows updated.

The Win32::ODBC man page says:
Sql( SQL_STRING )

Executes the SQL command SQL_STRING on the current connection.
Returns ? on success, or an error number on failure.

but as far as I can tell, Sql() isn't returning anything after my update query.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to get number of rows updated in by an sql statement? (Win32::ODBC)
by Anonymous Monk on Feb 01, 2004 at 20:31 UTC
    From http://www.roth.net/perl/odbc/docs/ODBC_Docs.htm#RowCount_:
    RowCount(); RowCount($Connection);

    For UPDATE, INSERT, and DELETE statements the returned value is the number of rows affected by the request or -1 if the number of affected rows is not available.

    Note: This function is not supported by all ODBC drivers! Some drivers do support this but not for all statements (eg. it is supported for UPDATE, INSERT and DELETE commands but not for the SELECT command).

    Note: Many data sources cannot return the number of rows in a result set before fetching them; for maximum interoperability, applications should not rely on this behavior.

    Returns:

    Number of affected rows or -1 if not supported by driver in the current context.

Re: How to get number of rows updated in by an sql statement? (Win32::ODBC)
by jreades (Friar) on Jan 15, 2001 at 02:56 UTC

    The documentation for DBI is pretty good on this front.

    It looks like:

    $rows = $sth->rows;

    ... would return what you're looking for (unless the ODBC driver doesn't support this kind of method).

    If the driver is very limited (I haven't worked with the Win32::ODBC driver) then you might have to fudge it a bit and try a clever SELECT statement based on the UPDATE if you really need to know how many rows were affected.

      that wasn't quite it. You can ignore me because I was suffering from a severe case of 24 hour blindness:P Your answer pointed me in the right direction (In the same documentation I thought I had looked through completely enough)

      none the less, I appreciate you took the time to answer my question despite my inadequate research:)

      All those here for the roshambo, please line up to the right....