Solved

PL/SQL Update Command

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

724 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