Solved

Help with updating column in one table with values from table in another table.

Posted on 2013-12-17
2
211 Views
Last Modified: 2013-12-18
I have two tables (TableA and TableB).  I would like to update the values in TableA NEWCOLUMN to match the values in TableB OLDCOLUMN when ID is equal.  NEWCOLUMN and OLDCOLUMN are geometry columns.

TableA

ID  | NEWCOLUMN
0   |
1   |
2   |
3   |
4   |
5   |


TableB


ID  | OLDCOLUMN
------------------
1   | a
2   | b
3   | c
4   | d
5   | e
6   | f
7   | g
8   | h



I had used the following query in the past, but am now getting an error when I try to update.

UPDATE TableA
SET TableA.NEWCOLUMN = (SELECT TableB.OLDCOLUMN FROM TableB WHERE TableA.ID = TableB.ID)

Open in new window



I get the following error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
0
Comment
Question by:VAMS1
  • 2
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39724148
Give this a whirl..
UPDATE TableA
SET TableA.NEWCOLUMN = TableB.OLDCOLUMN
FROM TableA
   JOIN TableB ON TableA.ID = TableB.ID

Open in new window

btw Nice job laying out your question.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39724167
>Subquery returned more than 1 value.
btw The reason for the error is whenever you have a 'SET column_name = (subquery)', when we're assigning the return set of a query to a scalar variable, the (subquery) can only return one value.

>SELECT TableB.OLDCOLUMN FROM TableB WHERE TableA.ID = TableB.ID
Which returned every ID where there was a match, hence the error.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question