Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with subqueries in update statement 1

Status
Not open for further replies.

sql99

Programmer
Nov 12, 2003
83
US
Hello,

I have the following update statement:

UPDATE person per
SET per.start_date = (SELECT emp.start_date
FROM employee emp
WHERE per.ident = emp.ident);


Can you please tell me why this update statement isn't working? It's updating all the start_dates in the person table instead of the 20 that are in the employee table even though I specified the ident columns to match.

Thanks in advance,
sql99
 
SQL,

The problem is with SQL's "two-stage" treatment of your update. We'll call "Stage 1" the "update-value-gathering subquery" and "Stage 2" (which is missing from your Update) is the "range-limiting subquery". Since you are missing "Stage 2", Oracle's default result is to update every row in the table. Here, then, is how you can accomplish what you want:
Code:
 UPDATE person per
     SET per.start_date = (SELECT emp.start_date 
                              FROM employee emp
                             WHERE per.ident = emp.ident)
     WHERE exists = (SELECT 'x' FROM employee emp
                      WHERE per.ident = emp.ident);
Try that and let us know if it resolves your need.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
@ 22:44 (06Dec04) UTC (aka "GMT" and "Zulu"),
@ 15:44 (06Dec04) Mountain Time
 
Silly me...as soon as I hit the [Submit] key, I noticed a slight syntax error in my code suggestion: there should be no "=" following "exists". It should read:
Code:
UPDATE person per
     SET per.start_date = (SELECT emp.start_date 
                              FROM employee emp
                             WHERE per.ident = emp.ident)
     WHERE exists (SELECT 'x' FROM employee emp
                      WHERE per.ident = emp.ident);

Sorry.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
@ 22:48 (06Dec04) UTC (aka "GMT" and "Zulu"),
@ 15:48 (06Dec04) Mountain Time
 
Thanks Dave. Of course it worked....I really appreciate your help...

sql99
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top