There are two tables in my database
1. Table_A
2. Table_B
Structure for Table_A - A_ID - A_code(FK to B_ID)
-
1001 - Null
-
1002 - Null
-
1003 - Null
-
Structure for Table_B - B_ID - B_A_ID (FK to A_ID)
-
501 - 1001
-
502 - 1003
-
503 - 1002
-
I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored. -
-
UPDATE Table_A SET A_code = (select
-
Table_B.B_ID
-
from Table_B
-
Inner join Table_A on
-
Table_A.A_ID=Table_B.B_A_ID
-
Where Table_B.B_A_ID = 1001)
-
Where A_ID = 1001
Then how to update column A_code at once?
7 3787
There are two tables in my database
1. Table_A
2. Table_B
Structure for Table_A - A_ID - A_code(FK to B_ID)
-
1001 - Null
-
1002 - Null
-
1003 - Null
-
Structure for Table_B - B_ID - B_A_ID (FK to A_ID)
-
501 - 1001
-
502 - 1003
-
503 - 1002
-
I need to update A_code column with B_ID with the according to the relation ship.this might be work for single recored. -
-
UPDATE Table_A SET A_code = (select
-
Table_B.B_ID
-
from Table_B
-
Inner join Table_A on
-
Table_A.A_ID=Table_B.B_A_ID
-
Where Table_B.B_A_ID = 1001)
-
Where A_ID = 1001
Then how to update column A_code at once?
- 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
- 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
- UPDATE Table1 INNER JOIN Table2 ON Table1.A_ID = Table2.B_A_ID SET Table1.A_COde = Table2.B_ID
-
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))
I create a sample table with this Queries.But failed when try to run the update. -
-
Create Table Table_1(
-
ID int primary key Identity (1001,1),
-
Code varchar(10) null
-
)
-
Go
-
-
insert into Table_1(Code)values(null)
-
insert into Table_1(Code)values(null)
-
insert into Table_1(Code)values(null)
-
-
Go
-
Create Table Table_2(
-
ID int primary key Identity (501,1),
-
FK_ID int not null,
-
FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
-
)
-
Go
-
insert into table_2(FK_ID)values(1001)
-
insert into table_2(FK_ID)values(1002)
-
insert into table_2(FK_ID)values(1003)
-
Go
-
select * from table_1
-
select * from table_2
-
-
Update Query -
-
-
-
UPDATE Table_1 INNER JOIN Table_2
-
ON Table_1.ID = Table_2.FK_ID
-
SET Table_1.Code = Table_2.ID
-
Please try it and help me to solve this.
Thanks a Lot!
I create a sample table with this Queries.But failed when try to run the update. -
-
Create Table Table_1(
-
ID int primary key Identity (1001,1),
-
Code varchar(10) null
-
)
-
Go
-
-
insert into Table_1(Code)values(null)
-
insert into Table_1(Code)values(null)
-
insert into Table_1(Code)values(null)
-
-
Go
-
Create Table Table_2(
-
ID int primary key Identity (501,1),
-
FK_ID int not null,
-
FOREIGN KEY (FK_ID) REFERENCES Table_1(Id)
-
)
-
Go
-
insert into table_2(FK_ID)values(1001)
-
insert into table_2(FK_ID)values(1002)
-
insert into table_2(FK_ID)values(1003)
-
Go
-
select * from table_1
-
select * from table_2
-
-
Update Query -
-
-
-
UPDATE Table_1 INNER JOIN Table_2
-
ON Table_1.ID = Table_2.FK_ID
-
SET Table_1.Code = Table_2.ID
-
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
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) -
UPDATE table_1
-
SET Code = table_2.ID
-
FROM TABLE_2
-
WHERE TABLE_1.ID = TABLE_2.FK_ID
Or Any issues with this?
Does this one do the same thing as your one?
(see my previous post for structure of tables) -
UPDATE table_1
-
SET Code = table_2.ID
-
FROM TABLE_2
-
WHERE TABLE_1.ID = TABLE_2.FK_ID
Or Any issues with this?
You can do it in this way also.
You can do it in this way also.
Many Thanks! then no need to struggle with Joins.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: shottarum |
last post by:
I currently have 2 tables as follows:
CREATE TABLE .
(
mhan8 int,
mhac02 varchar(5),
mhmot varchar(5),
mhupmj int
)
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
| |