Solved

SQL update problem

Posted on 2011-09-12
3
306 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:ewangoya
ewangoya 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now