Solved

PL/SQL Update Command

Posted on 2006-10-19
4
3,329 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

707 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

12 Experts available now in Live!

Get 1:1 Help Now