0

In PHP, mysql_affected_rows() tells me how many rows in a table where changed after a MySQL UPDATE statement. It only counts rows where values actually changed.

The MSSQL equivalent, mssql_rows_affected(), however, returns the number of rows where the WHERE clause of the UPDATE statement is true, even if nothing in those rows actually changes.

Is there a way to determine the number of rows that were actually changed in MSSQL?

1
  • I don't understand. Surely if the WHERE clause evaluates to true, then the row is UPDATEd. Jan 21, 2011 at 23:18

2 Answers 2

1

You can add a clause to your WHERE condition to not update a row if it doesn't change:

UPDATE yourtable
SET foo = 'bar'
WHERE yourcondition
AND foo <> bar
1
  • Works great, only needed an additional clause to handle the case where foo is NULL.NULL <> bar returns false and so lines that contain NULL never update.
    – jms
    Jan 26, 2011 at 11:25
0

You can use mysql_affected_rows

int mysql_affected_rows ([ resource $link_identifier ] )

More information at : http://php.net/manual/en/function.mysql-affected-rows.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.