If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Updating

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-03, 14:30
exdter exdter is offline
Registered User
 
Join Date: Aug 2003
Posts: 328
Updating

I am trying to update a table using data from another table. Table ones columns are code1,name1. Table twos columns are code2,name2. Some of the code1's are null and I want to take the code2's and insert them where table1.name1=table2.name2. The problem is is that table has in some places 2 different codes for the same name, and of course I get single row subquery returns more than one row. Is there some way around this? I'm guessing that because there are 2 different code2's for the same name, the insert query can't make up its mind as to which to insert. I am running SQL Server 2000 on XP.
Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-16-03, 15:19
simarjitraina simarjitraina is offline
Registered User
 
Join Date: Nov 2003
Posts: 11
" where table1.name1=table2.name2. "

aren't u suppose to use joins for these , outer and inner joins. i think u suppose to use inner joins

this statement "table1.name1=table2.name2." i don think it is goona work
Reply With Quote
  #3 (permalink)  
Old 12-16-03, 15:29
exdter exdter is offline
Registered User
 
Join Date: Aug 2003
Posts: 328
I think my problem is that I have:

Table1 Table2
code1 name1 code2 name2
null Bob 23 Bob
null Bob 27 Bob
null Bob 526 Bob

and the update query can't decide which code2 to put into the null in code1.
Reply With Quote
  #4 (permalink)  
Old 12-17-03, 06:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
That's correct. The best you can do is pick an arbitrary code2 value and use that - for example:

update table1
set code1 = (select min(code2) from table2 where table2.name2 = table1.name1)
where code1 is null;

Whether the code picked will be right or wrong is anyone's guess!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 12-17-03, 09:02
exdter exdter is offline
Registered User
 
Join Date: Aug 2003
Posts: 328
That's what I thought. Thanks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On