473,395 Members | 1,766 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,395 software developers and data experts.

Update a column in a table using the values in another table

ak1dnar
1,584 Expert 1GB
There are two tables in my database
1. Table_A
2. Table_B


Structure for Table_A
Expand|Select|Wrap|Line Numbers
  1.  A_ID - A_code(FK to B_ID) 
  2. 1001 - Null
  3. 1002 - Null
  4. 1003 - Null
  5.  
Structure for Table_B
Expand|Select|Wrap|Line Numbers
  1.  B_ID - B_A_ID (FK to A_ID) 
  2. 501 - 1001 
  3. 502 - 1003 
  4. 503 - 1002 
  5.  
I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored.

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE Table_A SET A_code = (select 
  3. Table_B.B_ID
  4. from Table_B
  5. Inner join Table_A on 
  6. Table_A.A_ID=Table_B.B_A_ID 
  7. Where Table_B.B_A_ID = 1001)
  8. Where A_ID = 1001
Then how to update column A_code at once?
Jul 18 '07 #1
7 3787
hariharanmca
1,977 1GB
There are two tables in my database
1. Table_A
2. Table_B


Structure for Table_A
Expand|Select|Wrap|Line Numbers
  1.  A_ID - A_code(FK to B_ID) 
  2. 1001 - Null
  3. 1002 - Null
  4. 1003 - Null
  5.  
Structure for Table_B
Expand|Select|Wrap|Line Numbers
  1.  B_ID - B_A_ID (FK to A_ID) 
  2. 501 - 1001 
  3. 502 - 1003 
  4. 503 - 1002 
  5.  
I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored.

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE Table_A SET A_code = (select 
  3. Table_B.B_ID
  4. from Table_B
  5. Inner join Table_A on 
  6. Table_A.A_ID=Table_B.B_A_ID 
  7. Where Table_B.B_A_ID = 1001)
  8. Where A_ID = 1001
Then how to update column A_code at once?

Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = [Table2].[B_ID]


concider Table_A as Table1
Table_B as Table2

i think this will help you
Jul 18 '07 #2
hariharanmca
1,977 1GB
Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = [Table2].[B_ID]


concider Table_A as Table1
Table_B as Table2

i think this will help you

Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = Table2.B_ID
  2. WHERE (((Table1.A_ID) Like '1001'))
this will work for 1 record, which is 1001

if Table1.A_ID is text type then WHERE (((Table1.A_ID) Like '1001'))
if Table1.A_ID is Number type then WHERE (((Table1.A_ID) = 1001))
Jul 18 '07 #3
ak1dnar
1,584 Expert 1GB
I create a sample table with this Queries.But failed when try to run the update.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Create Table Table_1(
  3. ID int primary key Identity (1001,1),
  4. Code varchar(10) null 
  5. )
  6. Go
  7.  
  8. insert into Table_1(Code)values(null)
  9. insert into Table_1(Code)values(null)
  10. insert into Table_1(Code)values(null)
  11.  
  12. Go 
  13. Create Table Table_2(
  14. ID int primary key Identity (501,1),
  15. FK_ID int not null,
  16. FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
  17. )
  18. Go
  19. insert into table_2(FK_ID)values(1001)
  20. insert into table_2(FK_ID)values(1002)
  21. insert into table_2(FK_ID)values(1003)
  22. Go
  23. select * from table_1
  24. select * from table_2
  25.  
  26.  
Update Query

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. UPDATE Table_1 INNER JOIN Table_2 
  5. ON Table_1.ID = Table_2.FK_ID 
  6. SET Table_1.Code = Table_2.ID
  7.  
Please try it and help me to solve this.
Thanks a Lot!
Jul 18 '07 #4
vijaii
15
I create a sample table with this Queries.But failed when try to run the update.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Create Table Table_1(
  3. ID int primary key Identity (1001,1),
  4. Code varchar(10) null 
  5. )
  6. Go
  7.  
  8. insert into Table_1(Code)values(null)
  9. insert into Table_1(Code)values(null)
  10. insert into Table_1(Code)values(null)
  11.  
  12. Go 
  13. Create Table Table_2(
  14. ID int primary key Identity (501,1),
  15. FK_ID int not null,
  16. FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
  17. )
  18. Go
  19. insert into table_2(FK_ID)values(1001)
  20. insert into table_2(FK_ID)values(1002)
  21. insert into table_2(FK_ID)values(1003)
  22. Go
  23. select * from table_1
  24. select * from table_2
  25.  
  26.  
Update Query

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. UPDATE Table_1 INNER JOIN Table_2 
  5. ON Table_1.ID = Table_2.FK_ID 
  6. SET Table_1.Code = Table_2.ID
  7.  
Please try it and help me to solve this.
Thanks a Lot!

Try this ...

UPDATE TABLE_A
SET A_CODE = B_ID
FROM TABLE_A INNER JOIN TABLE_B
ON A_ID = B_A_ID
Jul 18 '07 #5
ak1dnar
1,584 Expert 1GB
Try this ...

UPDATE TABLE_A
SET A_CODE = B_ID
FROM TABLE_A INNER JOIN TABLE_B
ON A_ID = B_A_ID
Does this one do the same thing as your one?
(see my previous post for structure of tables)

Expand|Select|Wrap|Line Numbers
  1. UPDATE table_1
  2. SET Code = table_2.ID
  3. FROM TABLE_2
  4. WHERE TABLE_1.ID = TABLE_2.FK_ID
Or Any issues with this?
Jul 18 '07 #6
vijaii
15
Does this one do the same thing as your one?
(see my previous post for structure of tables)

Expand|Select|Wrap|Line Numbers
  1. UPDATE table_1
  2. SET Code = table_2.ID
  3. FROM TABLE_2
  4. WHERE TABLE_1.ID = TABLE_2.FK_ID
Or Any issues with this?
You can do it in this way also.
Jul 18 '07 #7
ak1dnar
1,584 Expert 1GB
You can do it in this way also.
Many Thanks! then no need to struggle with Joins.
Jul 18 '07 #8

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

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
9
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.