473,414 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

UPDATE and SELECT Combination

2
I have two tables. Table A is the main table I need to update and table B is the update table with the new data. Table B contains only two fields- fielda (also contained in table A for the linking but also is the field that needs changed) and a second field (fieldb) which is the NEW value of fielda that I need to change in TableA. Thus I need a combination UPDATE/SELECT statement to link the two tables togeather to update table A with the new values (fieldb) from table B. I need a start on how I can do this. Thanks.
Nov 9 '09 #1
2 2801
nbiswas
149 100+
This may help.. I am providing 2 solutions based on my understanding of your question

Declare the two tables and insert some values to them

Expand|Select|Wrap|Line Numbers
  1. declare @tblA table(fieldA int)
  2.  
  3. declare @tblB table(fieldA int,fieldB int)
  4. insert into @tblA 
  5.     select 1 union all select 2 union all select 3 union all
  6.     select 4 union all select 5
  7.  
  8. insert into @tblB 
  9.     select 1,10 union all select 2,20 union all select 10,2 union all 
  10.     select 30,30 union all select 3,30
Solution 1:

Expand|Select|Wrap|Line Numbers
  1. update @tblA 
  2. set fieldA  =  X.TblBFieldB
  3. from 
  4. (select t2.fieldA TblBFieldA,t2.fieldB TblBFieldB from @tblA t1 
  5. inner join  @tblB t2
  6. on t1.fieldA = t2.fieldA) X(TblBFieldA,TblBFieldB)
  7. where fieldA = X.TblBFieldA  
  8. select * from @tblA 

Solution 2:

Expand|Select|Wrap|Line Numbers
  1. ;with cte as
  2. (select t2.fieldA TblBFieldA,t2.fieldB TblBFieldB from @tblA t1 
  3. inner join  @tblB t2
  4. on t1.fieldA = t2.fieldA
  5. )
  6. update @tblA 
  7. set fieldA  =  cte.TblBFieldB
  8. from cte
  9. where fieldA = cte.TblBFieldA  
  10. select * from @tblA 
Let me know in case of any concern
Nov 10 '09 #2
rickcf
2
Perfect! Just the start I needed! Here is the working code I got to work in case anyone else should need it:


update tablea
set tablea.fielda = X.newfield from
(select t2.oldfield, t2.newfield from tablea t1
inner join tableb t2
on t1.fielda = t2.oldfield) X
where tablea.fielda = X.oldfield


Thanks!

Rick
Nov 10 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Murali | last post by:
Hi All I was reading thro the posting(s) of Thomas Kyte and his nifty approach to doing updates without the need for unnecessary correlated subqueries. An alternative to correlated subquery...
3
by: SINTECO - Andrea Tartaglia - NEWS | last post by:
Hallo all, I'm quite new to SQL and I have a problem updating some fields in a table, maybe some of you in this forum can help me! This SELECT statement returns the records that I need to...
5
by: devi | last post by:
hi, I am creating a simple bug tracker application (in Access db) and i created a hisotry table to log the bug history. The history table contains details like ProblemRecordNo (PRN),...
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
2
by: shorti | last post by:
Hello again. I previously inquired about updating 1000 records at a time in a script (not using cursors). Now I have to take it one step further. I want to update records from two different...
5
by: Sphenix | last post by:
------------------------ UPDATE A SET A.ID = '?' + A.ID FROM TABLEA A LEFT OUTER JOIN TABLEB B ON A.INDEX = B.INDEX WHERE B.DUP_ID IS NULL ------------------------ seems like update with...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
11
by: cooperkuo | last post by:
Dear all, I have a question about ADO in the subform. I know how to use ADO to insert/update/select data into the sigin form, but wehn I try to do it in the form with subform((Datasheet). I don't...
3
by: modernshoggoth | last post by:
G'day all, I'm trying to update a single table (containing plans for flights) with information from the same and other tables (containing info for organisations and locations). tblFlightPlans...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.