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

Update multiple fields Accross a Join

"David Portas" <snipped for brevity> wrote:

Example 1:

UPDATE table_a
SET col = ? /* Unspecified */
WHERE EXISTS
(SELECT *
FROM table_b
WHERE table_b.key_col = table_a.key_col)

<snip again>

Many thanks. I have used this sample extensively since you posted it. Hope
you (or someone) can help me with one more thing: How would it be written
to update several fields in table A with data from table B, where as you
have shown, a column matches the records?

TIA!

~ Duane Phillips.
Jul 23 '05 #1
4 16380
Duane Phillips (as***@askme.askme) writes:
Many thanks. I have used this sample extensively since you posted it.
Hope you (or someone) can help me with one more thing: How would it be
written to update several fields in table A with data from table B,
where as you have shown, a column matches the records?


UPDATE table_a
SET col1 = b.that_col,
col2 = b.this_col,
col3 = b.yet_another_col,
...
FROM table_a a
JOIN table_b b ON a.key_col = b.key_col

This presumes that for a row in table_a there is at most one matching
row in table_b.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
On Thu, 20 Jan 2005 15:57:49 -0700, Duane Phillips wrote:
"David Portas" <snipped for brevity> wrote:

Example 1:

UPDATE table_a
SET col = ? /* Unspecified */
WHERE EXISTS
(SELECT *
FROM table_b
WHERE table_b.key_col = table_a.key_col)

<snip again>

Many thanks. I have used this sample extensively since you posted it. Hope
you (or someone) can help me with one more thing: How would it be written
to update several fields in table A with data from table B, where as you
have shown, a column matches the records?

TIA!

~ Duane Phillips.


Hi Duane,

The "official" (ANSI-standard) answer is:

UPDATE table_a
SET col1 = (SELECT col1
FROM table_b
WHERE table_b.key_col = table_a.key_col)
, col2 = (SELECT col2
FROM table_b
WHERE table_b.key_col = table_a.key_col)
, col3 = (SELECT col3
FROM table_b
WHERE table_b.key_col = table_a.key_col)
WHERE EXISTS (SELECT *
FROM table_b
WHERE table_b.key_col = table_a.key_col)

Unfortunately, SQL Server doesn't optimize this too well. You could also
use the proprietary T-SQL UPDATE FROM syntax:

UPDATE table_a
SET col1 = table_b.col1,
col2 = table_b.col2,
col3 = table_b.col3
FROM table_a
INNER JOIN table_b
ON table_b.key_col = table_a.key_col

The results will only be the same if each row in table a is matched by
exactly one row in table b; in other cases, the results will be different
(and the results of the UPDATE FROM syntax may even be undefined)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #3
Thankyou!

~ Duane Phillips.

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:9q********************************@4ax.com...
On Thu, 20 Jan 2005 15:57:49 -0700, Duane Phillips wrote:
"David Portas" <snipped for brevity> wrote:

Example 1:

UPDATE table_a
SET col = ? /* Unspecified */
WHERE EXISTS
(SELECT *
FROM table_b
WHERE table_b.key_col = table_a.key_col)

<snip again>

Many thanks. I have used this sample extensively since you posted it.
Hope
you (or someone) can help me with one more thing: How would it be written
to update several fields in table A with data from table B, where as you
have shown, a column matches the records?

TIA!

~ Duane Phillips.


Hi Duane,

The "official" (ANSI-standard) answer is:

UPDATE table_a
SET col1 = (SELECT col1
FROM table_b
WHERE table_b.key_col = table_a.key_col)
, col2 = (SELECT col2
FROM table_b
WHERE table_b.key_col = table_a.key_col)
, col3 = (SELECT col3
FROM table_b
WHERE table_b.key_col = table_a.key_col)
WHERE EXISTS (SELECT *
FROM table_b
WHERE table_b.key_col = table_a.key_col)

Unfortunately, SQL Server doesn't optimize this too well. You could also
use the proprietary T-SQL UPDATE FROM syntax:

UPDATE table_a
SET col1 = table_b.col1,
col2 = table_b.col2,
col3 = table_b.col3
FROM table_a
INNER JOIN table_b
ON table_b.key_col = table_a.key_col

The results will only be the same if each row in table a is matched by
exactly one row in table b; in other cases, the results will be different
(and the results of the UPDATE FROM syntax may even be undefined)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 23 '05 #4
Thankyou!

~ Duane Phillips.

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.0.0.1...
Duane Phillips (as***@askme.askme) writes:
Many thanks. I have used this sample extensively since you posted it.
Hope you (or someone) can help me with one more thing: How would it be
written to update several fields in table A with data from table B,
where as you have shown, a column matches the records?


UPDATE table_a
SET col1 = b.that_col,
col2 = b.this_col,
col3 = b.yet_another_col,
...
FROM table_a a
JOIN table_b b ON a.key_col = b.key_col

This presumes that for a row in table_a there is at most one matching
row in table_b.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Melissa | last post by:
Products appear on multiple orders in a batch of orders. I created a totals query with the fields, ProductID and SumOfQuantity. I have a TblProduct table of products with the fields ProductID,...
2
by: anony | last post by:
Maybe this feature is already out there. I guess you could write triggers to do some of this. Often when designing a database I add a start_date and end_date column to the table. The start_date...
3
by: Antonio | last post by:
Can somebody tell my why the following procedure changes the data in the fields being updated to all the records in the database? private void updateRow(object source,...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
1
by: racquetballer | last post by:
I need to to an update query that involves three tables: table Dealer needs to be updated with data from table Personnel and table Title. Dealer is joined to Personnel where Dealer.Dealer_Code =...
5
by: Ferasse | last post by:
Hi, I'm an occasional Ms-Access developer, so there is still a lot of stuff that I don't get... Right now, I'm working on a database that stores contractual information. One of the form that...
2
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma...
13
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.