Using the SAVEPOINT statement, you can mark a spot in the middle of a transaction to which you might wish to roll back. ASAVEPOINTstatement allows you to undo part of a transaction rather than all of it. Following is the syntax to use in creating a savepoint: SAVEPOINT savepoint_name; You uniquely name the savepoint and then may subsequently reference it in aROLLBACK statement as follows: ROLLBACK TO SAVEPOINT savepoint_name; Regardless of how many savepoints exist within a transaction, aROLLBACK statement without aSAVEPOINTreference will automatically roll back the entire transaction. The following SQL*Plus example uses a savepoint to conditionally undo the DML statements since theSAVEPOINTstatement was issued. First, the example creates a new region in theREGIONStable because you are opening a branch office in Antarctica: SQL> insert into regions (region_id, region_name) 1 row created. SQL> savepoint new_region; Savepoint created. The savepointNEW_REGIONmay come in handy later if you need to roll back the insertion of the new countries you will be adding in the next few statements: SQL> insert into countries (country_id, country_name, region_id) 1 row created. SQL> savepoint new_country_1; Savepoint created. SQL> insert into countries (country_id, country_name, region_id) 1 row created. SQL> savepoint new_country_2; Savepoint created. After performing these two inserts, you realize that you have a geographic anomaly on your hands, and that for now you are just going to add one country to theCOUNTRIEStable but keep the new region in theREGIONStable: SQL> rollback to new_region; Rollback complete. SQL> insert into countries (country_id, country_name, region_id) 1 row created. Just to double-check your work before you run theCOMMITstatement, you query theREGIONS andCOUNTRIEStables: SQL> select * from regions; REGION_ID REGION_NAME SQL> select * from countries; CO COUNTRY_NAME REGION_ID CO COUNTRY_NAME REGION_ID CO COUNTRY_NAME REGION_ID 26 rows selected. SQL> Now that everything looks good, you commit the single-row change to theREGIONStable, and the single-row change to theCOUNTRIES table: SQL> commit; Commit complete.
blog comments powered by Disqus |
|
|
|
|
|
|
|