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 > Update query Issue

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-08, 10:57
cgbi cgbi is offline
Registered User
 
Join Date: Apr 2008
Posts: 4
Update query Issue

Hi,

I am trying to run an update query against a limited number of records in a table where a corresponding record exists in another table.


update agency_cd_ctrl a
set a.user_id = (select b.user_id
from seller_imp b
where a.user_id = b.alias_user_id)


The problem i am having is that the code is updating all records in the update table (sets user_id to null where there is no match) - i was hoping just to update the records where there was a match (about 60 records out of 3000) and update to the value from the 2nd table.

Can anyone point me in the right direction please?

Thanks
James
Reply With Quote
  #2 (permalink)  
Old 04-23-08, 15:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
this is the ANSI SQL forum

first thing you have to do is tell us which database system you're using

UPDATE syntax varies too much from one to the next

once we know which database you're using, we'll move this thread to the appropriate forum and you can get the specific help you need

i'm going to guess SQL Server 2000
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-24-08, 00:29
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
As r937 pointed out, it would help to know which SQL data engine you are using. The UPDATE statement has a lot of vendor/dialect specific add-on features that allow for more concise coding than the ANSI standard permits.

To use the pure ANSI SQL to get what you've described:
Code:
UPDATE agency_cd_ctrl
   SET user_id = (SELECT b.user_id
      FROM seller_imp AS b
      WHERE  b.alias_user_id = agency_cd_ctrl.user_id)
   WHERE EXISTS (SELECT *
      FROM seller_imp AS c
      WHERE  c.alias_user_id = agency_cd_ctrl.user_id)
-PatP
Reply With Quote
  #4 (permalink)  
Old 04-24-08, 03:53
cgbi cgbi is offline
Registered User
 
Join Date: Apr 2008
Posts: 4
Hi,

Thanks for the response - i'm trying to run the query against an Oracle 9i database.

The code below did the trick - thanks for your help...

James


update agency_cd_ctrl
set user_id = (select b.user_id
from seller_imp b
where b.alias_user_id = agency_cd_ctrl.user_id)
where exists (select *
from seller_imp c
where c.alias_user_id = agency_cd_ctrl.user_id)
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