?
Solved

PL/SQL Update Command

Posted on 2006-10-19
4
Medium Priority
?
3,336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 1000 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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

752 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