Solved

SQL simple update query

Posted on 2008-10-16
4
198 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
[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
  • 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 300 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Sql Permission 6 73
Urgent help needed! Unable to to paste in query designer 29 55
How can I use this function? 3 32
How come this XML node is not read? 3 28
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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