Solved

PL/SQL Update Command

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PL/SQL Search for multiple strings 5 61
PL/SQL Display based on value 4 30
migration MS SQL database to Oracle 30 63
SYS password changed. Now can't log in as SYS 27 18
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

831 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