Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

How to get number of rows updated in by an sql statement? (Win32::ODBC)

by 2501 (Pilgrim)
on Jan 13, 2001 at 00:35 UTC ( #51466=categorized question: print w/replies, xml ) Need Help??
Contributed by 2501 on Jan 13, 2001 at 00:35 UTC
Q&A  > database programming


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:

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.

Answer: How to get number of rows updated in by an sql statement? (Win32::ODBC)
contributed by Anonymous Monk


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.


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

Answer: How to get number of rows updated in by an sql statement? (Win32::ODBC)
contributed by jreades

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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (6)
    As of 2019-10-16 14:24 GMT
    Find Nodes?
      Voting Booth?