Solved

SQL update problem

Posted on 2011-09-12
3
310 Views
Last Modified: 2012-05-12
I'm having a trouble with a simple updating statement.

I have two tables:
Table1 columns -
id1
itemId
reporterName
other
...

Table2 columns -
id2
itemId
firstName
lastName
otherfld
...

itemId in the two tables mean the same field. I need to select firstName and lastName from Table2 and insert "firstName, lastName " in Table1.

Here's my quesry -

update dbo.Table1
set reporterName=(
      select nn.lastName + ', ' + nn.firstName
      from dbo.Table2 nn inner join dbo.Table1 t on t.itemId=nn.itemId )
where itemId in (select rn.itemId from Table2 rn inner join dbo.Table1 tt on tt.itemId=rn.itemId)
and reporterName is null

I got the error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Please help. Thanks.
0
Comment
Question by:minglelinch
3 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 250 total points
ID: 36526038

Hi,

I think this is what you looking for

update Table1
set reporterName=t2.lastName + ', ' + t2.firstName
from Table1 t1
join Table2 t2 on t1.itemId=t2.itemId
where t1.reporterName is null


/peter
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 250 total points
ID: 36526538
try
update Table1
set ReportedName := ISNULL(t2.lastName + ', ', '',) + ISNULL(t2.firstName, '')
from Table2
inner join Table1 on (Table1.itemId = Table2.ItemID)
and Table1.reporterName is null

Open in new window

0
 
LVL 1

Author Closing Comment

by:minglelinch
ID: 36529178
Nice solution. Thank you both.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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