Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to update a field in one table using data from a second table as a criteria?

Posted on 2008-10-07
6
Medium Priority
?
267 Views
Last Modified: 2013-12-07
I have two tables, diploma and degree with columns and data as follows:

Diploma Table                                                                  Degree Table

ID     SeqNo  Order Date                                                  ID       SeqNo   Grad_Code  Term_Code
---     -------   --------------                                                  ---       --------    -------------   --------------
123   1                                                                             123     1             GR              200800
123   2                                                                             123     2             WA             200810
456   1                                                                             456     1             GR              200820

I would like to update the Order Date in the Diploma table with the system date based on the matching ID value and seq number in the Degree table with Grad_Code  = 'GR' and Term_Code = '200800'. How can I write an SQL statement to accomplish this? If so how? If not, can how can I do it using PL/SQL with cursors?
0
Comment
Question by:geeta_m9
[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
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22664993
update Diploma
set [Order Date] = getdate()
where exists ( select 1 from [Degree Table] b
     where Diploma.ID = b.ID
     and     Diploma .SeqNo = b.SeqNo
     and     b.Grad_Code  = 'GR' and b.Term_Code = '200800'
)

0
 
LVL 9

Accepted Solution

by:
jamesgu earned 2000 total points
ID: 22664996
changed to oracle syntax

update Diploma
set  Order_Date = sysdate
where exists ( select 1 from  Degree  b
     where Diploma.ID = b.ID
     and     Diploma .SeqNo = b.SeqNo
     and     b.Grad_Code  = 'GR' and b.Term_Code = '200800'
)
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22665007
See if this helps:
(uses a subquery to go get value -- if doesn't match will set to NULL)
Update Diploma di
Set di.[Order Date] = (SELECT SysDate FROM Degree de WHERE de.ID = di.ID AND de.SeqNo = di.SeqNo AND Grad_Code = 'GR' and Term_Code = '200800');

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22665011
I like jamesqu's approach without subquery!
0
 

Author Comment

by:geeta_m9
ID: 22672056
Thank you!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22672729
You are welcome!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

670 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