Solved

SQL simple update query

Posted on 2008-10-16
4
197 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…

840 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