Solved

PL/SQL Update Command

Posted on 2006-10-19
4
3,330 Views
Last Modified: 2008-05-18
Question,
         I need to update 1 field in 844 records changing the field from a 3 to a 1, my question is this, I am new to plsql and need to know what the safest way of updating records in oracle is, is it best to use a selection statement followed by an update statement or just an update statement with the (where in)  statement with the record identifiers.

what i really need is a way of being able to test the data selection before i update to make sure it will update only the records i want and to be able to run the update knowing what it is going to get updated is only what was selected above and not all records.


Thanks.


0
Comment
Question by:eenderle
4 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
ID: 17770107
Hi,

An update with a where in should be safest,

If you are using a transaction based table then you could run the update - check your data and if satisfied then commit your transaction, alternativley you could roll it back.

Or you could make a copy of your table into a temporary table and run the update there.  This is personal preference.
0
 
LVL 27

Expert Comment

by:sujith80
ID: 17771583
If you are using the same where clause for selecting the data and updating then no need to worry about it.
from a pl/sql block you can print the number of records updated like below.

set serveroutput on
begin
<your update statement>;
dbms_output.put_line(SQL%rowcount);
end;
/

So, you can run the select statement before the update and match the count with the value printed from the above block.
0
 
LVL 6

Accepted Solution

by:
makhan earned 250 total points
ID: 17772457
Hi,

First do a select statement with a where condition i.e.

select col1, col2
  from tab1
 where colx between 1 and 100

this will return <n> rows.

next use the update.

update tab1
     set col1 = <value>
 where colx between 1 and 100

this will update <n> rows

compare <n> in the first step with <n> in the second step.

these two should be equal.

Regards,

makhan

0
 
LVL 22

Expert Comment

by:earth man2
ID: 17772694
You may want to lock the rows before you update them if you have to worry bout multiuser issues.
ie select .. for update;
update;
commit;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to take different types of Oracle backups using RMAN.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now