Return one “ records updated” from multiple update queries
Disclaimer: I don't have much SQL experience, and I don't know what version of Oracle my company is using.
I have a product price table that needs to be updated tomorrow. There are several thousand records that will be getting updated, but still a thousand more that will go untouched.
Right now I have an update statement for every single record, and while I can run them all as a script I would like to get some sort of feedback as to how many records were updated, and if possible what statements failed.
If I run what I have as a script I'll end up with some 5,000 "1 row updated" messages with no clue as to which one failed if I see a "0 rows updated".
Is there some form of script I can use that will catch failures, or at the very least collect the number of rows updated into one total?
Does your product price table have some sort of modified date or last modified by fields? If so, make sure you're updating these.
Also, if using your script, you should set echo on and spool to a log file. Your script should look something like:
-- updates.sql set serveroutput on set echo on WHENEVER SQLERROR EXIT SQL.SQLCODE spool on spool updates.log -- put updates here commit; spool off
Log into sqlplus from the same directory as this file and run using: @updates.sql
The updates.log file will have each statement that is run.
If you wanted to get a total from a list of separate update statements, you'd need to inspect SQL%ROWCOUNT after each update. Something like (untested):
declare l_total pls_integer := 0; begin update product set x = 'foo' where y='bar'; l_total := l_total + SQL%ROWCOUNT; update product set x = 'blah' where y='someval'; l_total := l_total + SQL%ROWCOUNT; commit; dbms_output.put_line(l_total); end /
This is a basic pl/sql anonymous block, and can be used with the spool strategy above.
Finally, it is common to setup a basic log table, containing a message field, log datetime, some context fields (where the message is coming from, etc), type of msg (log, error), etc. Setup a procedure that handles inserting into the log table ( autonomous transaction), and then use the log procedure whenever you need to keep track of stats or whatever (dbms_output is very limited). Your environment might already have something like this, you just need to look for it (or ask a senior dev in your company).