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

standard sql: update multiple rows.

Hi,

Does SQL support update to multiple rows where values coming from a
sub-query?

e.g

insert into TABLE1
select column1, column2, column3 from TABLE2

This is perfectly valid, assumes TABLE1 has only three columns,
column1, column2, column3.
My question: Is there any way to UPDATE values to TABLE1 similarly?
something like

update TABLE1 set column1= ?, column2= ? , column3= ?
where .....
........... TABLE2
...........

OR is it that, sql allows only UPDATEs with one set of values to n
rows.

Can any one throw some light on this.

-Thanks and Regards,
Maymon.

Jan 10 '06 #1
3 49538
spartacus wrote:
Hi,

Does SQL support update to multiple rows where values coming from a
sub-query?

e.g

insert into TABLE1
select column1, column2, column3 from TABLE2

This is perfectly valid, assumes TABLE1 has only three columns,
column1, column2, column3.
My question: Is there any way to UPDATE values to TABLE1 similarly?
something like

update TABLE1 set column1= ?, column2= ? , column3= ?
where .....
.......... TABLE2
..........

OR is it that, sql allows only UPDATEs with one set of values to n
rows.

Can any one throw some light on this.

-Thanks and Regards,
Maymon.

You can do this two ways:
MERGE INTO T USING S ON T.pk = S.pk
WHEN MATCHED THEN UPDATE SET c1 = S.c1, c2 = S.c2

This is newly added and AFAIK supported only by Oracle 9i (+) and DB2
V8.1 for LUW (+)

In SQL92 (should work in MS SQL Server and any other SQL based RDBMS):

UPDATE T SET (c1, c2) = (SELECT c1, c2 FROM S WHERE pk = T.pk)
WHERE EXISTS(SELECT 1 FROM S WHERE pk = T.pk)

or use an IN predicate: UPDATE ... WHERE pk IN (SELECT pk FROM S)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 10 '06 #2
Serge Rielau (sr*****@ca.ibm.com) writes:
In SQL92 (should work in MS SQL Server and any other SQL based RDBMS):

UPDATE T SET (c1, c2) = (SELECT c1, c2 FROM S WHERE pk = T.pk)
WHERE EXISTS(SELECT 1 FROM S WHERE pk = T.pk)


This does not work in SQL Server. The syntax is:

UPDATE T
SET c1 = S.c1,
c2 = S.c2
FROM T
JOIN S ON T.pk = S.pk

The syntax is not very portable (works on Sybase too), but certainly
simple to grasp.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 10 '06 #3
Thanks serge,
It worked.

Jan 10 '06 #4

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

Similar topics

2
by: Rima | last post by:
I have the following two tables : table a (commit_id, capital_market_id, chg_lst_date ) table b (b_seq_id,
8
by: Steve | last post by:
Hi; Is there an **easy** way to tell tsql apart from standard sql? Will sqlserver run scripts written only in standard sql? What about variable definitions? Thanks in advance
7
by: aaj | last post by:
Hi all We had a small problem when an ASP web page had a missing 'where' statement and updated all the records in the table. Luckily we could retrieve all the data from the backups. How do...
12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
3
by: -Michelle- | last post by:
Hi Using A2003 on XP I am wondering from the MVP's and others, what is the most efficient way (in terms of time to process) of updating data in a table, using the docmd.RunSQL or Recordset ...
2
by: AH | last post by:
Dear all, I am facing this 'bug' that really drive me nut. I created a trigger for Update script and tested in Enterprise manager to ensure it function correctly. However, when I used both VB or...
5
by: Robert Brown | last post by:
Hi All. I have a routine that checks a SQL Table for all records 3 months prior to a predetermined date, then I insert them into an Archive DB then delete those records from the original table....
0
by: kolalakitty | last post by:
Hopefully someone here can help me/point me in the right direction. I've found tons of references towards making relations, creating rows, saving said rows, using datagrids, databinding objects,...
9
by: Keith G Hicks | last post by:
I have a vb app that runs on a server. It periodically checks for rows in a table that are ready to have data mailed out to clients. After it finds row(s) that are ready, it emails the info out and...
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
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
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
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
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,...

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.