Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL simple update query

Posted on 2008-10-16
4
Medium Priority
?
204 Views
Last Modified: 2012-05-05
I am trying update the following table:

id      SUB           pup          Atxt     btxt
----------------------------------------------
24    ENG           101          A          B
23     ENG          100          A         B
24    ENG           100          null       null
24    ENGLA       100          C          C


I want column Atxt and Btxt to be copied from records that have 'ENGLA' in column sub, TO records where column 'sub' has a value of ENG and where column 'pup' values match and only for records with 'id' value of 24

to end up with a table that looks like this:

id      SUB           pup          Atxt     btxt
----------------------------------------------
24    ENG           101          A          B
23     ENG          100          A         B
24    ENG           100          C         C
24    ENGLA       100          C          C

I was thinking of something like:

update table1

set atxt=btxt
where id = 24 and pup=pup  and sub =eng

but im lost with having 2 separate  where's in the query


Any suggestions?

0
Comment
Question by:nazateam
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22729466
What about this:
update t1
  set atxt= t2.atxt 
    , btxt = t2.btxt
from table1 t1
join table1 t2 
  on t2.id = t1.id
 and t2.pup = t1.pup
 and t2.atxt is null
where t1.id = 24
  and t1.atxt is not null

Open in new window

0
 

Author Comment

by:nazateam
ID: 22729536
Hmmm Perhaps, But where in there am i specifying the source of the update (records with 'ENGLA' under SUB and the destination (records with 'ENG' under SUB?


0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 22729621
yes, that's possible:
update t1
  set atxt= t2.atxt 
    , btxt = t2.btxt
from table1 t1
join table1 t2 
  on t2.id = t1.id
 and t2.pup = t1.pup
 and t2.atxt is not null
 and t2.sub = 'ENGA'
where t1.id = 24
  and t1.atxt is null
  and t1.sub = 'ENG'

Open in new window

0
 

Author Closing Comment

by:nazateam
ID: 31506662
Thanks for this, worked a treat!!
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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

916 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