473,398 Members | 2,380 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,398 software developers and data experts.

update from ...?

rob

Hey,

I need to update a row in one table with a row (most but not all
columns) from another table. This sounds like there should be a really
easy way to do this. I've seen there's FROM in the UPDATE statement but
haven't found any similar examples.

Basically, I need something like this pseudo-code:

update table2 set something = table1.something (??)
from table1
where table2.id = 150

but, i only need some of the columns from table1 into table2.

I've seen things like:

update table2 set
table2.col1 =
(select table1.col1 where table1.id = 10),
table2.col2 =
(select table1.col2 where table1.id = 10)
where table2.id = 10

....but this seems really verbose, because there's like a dozen columns.

Even a link to a tutorial is welcome, thanks!

:rob

Jul 23 '05 #1
6 2112
Try this:

update table2
set a.col1 = b.col1, a.col2 = b.col2, etc..
from table2 a
inner join table1 b on a.id = b.id
where a.id = 10

Jul 23 '05 #2
update table2 set
table2.col1 =
(select table1.col1 where table1.id = 10),
table2.col2 =
(select table1.col2 where table1.id = 10)
where table2.id = 10


Try this

UPDATE table2
SET table2.ColumnName=Table1.ColumnName, table2.columnNameB=table1.ColumnB
FROM Table1 INNER JOIN table2 ON table1.PrimaryKey=table2.PrimaryKey

When you want to update several columns just separate them with a comma like
shown, then join the two tables together in the FROM clause

Let me know how you get on,

Jayne
Jul 23 '05 #3
The answer is that it is verbose, but it works. The UPDATE..FROM
syntax is both proprietary and dangerous. This is a simple example
from Adam Machanic

CREATE TABLE Foo
(col_a CHAR(1) NOT NULL,
col_b INTEGER NOT NULL);

INSERT INTO Foo VALUES ('A', 0);
INSERT INTO Foo VALUES ('B', 0);
INSERT INTO Foo VALUES ('C', 0);

CREATE TABLE Bar
(col_a CHAR(1) NOT NULL,
col_b INTEGER NOT NULL);

INSERT INTO Bar VALUES ('A', 1);
INSERT INTO Bar VALUES ('A', 2);
INSERT INTO Bar VALUES ('B', 1);
INSERT INTO Bar VALUES ('C', 1);

You run this proprietary UPDATE with a FROM clause:

UPDATE Foo
SET Foo.col_b = Bar.col_b
FROM Foo INNER JOIN Bar
ON Foo.col_a = Bar.col_a;

The result of the update cannot be determined. The value of the column
will depend upon either order of insertion, (if there are no clustered
indexes present), or on order of clustering (but only if the cluster
isn't fragmented).

The best answer is for Microsoft to finally give us Standard SQL row
constructors in SQL-2005 so we could write:

UPDATE Foobar
SET (a,b,c, ..) = (SELECT x,y,z, .. FROM Barfoo WHERE ...);
WHERE ...;

Jul 23 '05 #4
--CELKO-- (jc*******@earthlink.net) writes:
The answer is that it is verbose, but it works. The UPDATE..FROM
syntax is both proprietary and dangerous.


Anything is dangerous. I've seen people wrong with the ANSI syntax as
well. I'd say it's even easier to go wrong with, because it much more
difficult to understand.

The purported problem with UPDATE FROM potentially qualifying multiple
rows is a red herring. If you feel compelled to write:

UPDATE tbl
SET col = (SELECT ...

You shouldn't use FROM in a SELECT either, or at least for one
table:

SELECT col = (SELECT ...
FROM tbl

If you join with incomplete conditions, you could get multiple rows
and cause a mess.

There is a nice beauty in the FROM clause for UPDATE: it makes the
language simpler to use, because the same construct is reused.

And this newsgroup is about SQL Server, and when it comes to SQL
Server, FROM is good practice, because it often gives much better
performance than the other syntax.
--
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
>> If you join with incomplete conditions, you could get multiple rows
and cause a mess. <<

Actually, you get a cardinality violation error and the process stops.
You know whenyou have a problem when you use the proper syntax.
And this newsgroup is about SQL Server, and when it comes to SQL

Server, FROM is good practice, because it often gives much better
performance than the other syntax. <<

I can get great performance if i do not have to get the right answer,
port or maintain code :)

Jul 23 '05 #6
--CELKO-- (jc*******@earthlink.net) writes:
If you join with incomplete conditions, you could get multiple rows

and cause a mess. <<

Actually, you get a cardinality violation error and the process stops.
You know whenyou have a problem when you use the proper syntax.


You don't. I was talking about:

SELECT ...
FROM ...
JOIN ...

That is the twin to the UPDATE FROM statement.

Sure, with a plain SELECT you may notice that you have too many rows,
but if the missing condition is on a 1-to-1.10 relationship you may
not see it.

And if you aggregate, for instance SUM or COUNT(*) you ge the same
number of rows as with the correct join. You just get incorrect
results.

Thus, if FROM is dangerous for UPDATE, it's dangerous for SELECT as
well.
--
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 #7

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

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...
27
by: VK | last post by:
<http://www.jibbering.com/faq/#FAQ3_2> The parts where update, replacement or add-on is needed are in <update> tag. 3.2 What online resources are available? Javascript FAQ sites, please...
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...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
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. ...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
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
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
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...
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...

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.