Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl: the Markov chain saw
 
PerlMonks  

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

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


Description:

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.

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

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.

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (11)
    As of 2014-04-21 15:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (495 votes), past polls