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 > Working With Multiple Queries

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-05, 19:40
fstop fstop is offline
Registered User
 
Join Date: Dec 2004
Posts: 22
Working With Multiple Queries

I have 2 queries (A & B), each containing multiple joins. For every row returned by B, I need to check to see if there is a matching row returned by A (match on EntryID). If there is a match, I need to update a specific field.

Can someone help me figure out how to set this up?
Reply With Quote
  #2 (permalink)  
Old 02-09-05, 01:38
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Basically, you want to do the update. That would be

UPDATE some_table SET column = some_value WHERE condition;

Can you include those queries into the 'condition'? Perhaps you could, but I guess it would be a giant condition having two queries, every containing multiple joins. Poor you if you try to put them together and poor everyone who'd try to debug this

I'd, somehow, prefer doing this using procedural options your DB engine provides. If it was Oracle, I'd use PL/SQL procedure with a cursor loop (for your B query) and then - for every row it returns - check existence of a matching row returned by the A query and do the required update.

Perhaps it is even possible to "join" those queries using EXISTS clause Oracle provides and do the job in the "condition" mentioned above ...

Well, those were some thoughts. You'll surely hear some more and decide what's the best approach for your problem.
Reply With Quote
  #3 (permalink)  
Old 02-09-05, 11:49
fstop fstop is offline
Registered User
 
Join Date: Dec 2004
Posts: 22
Thanks for your reply. I'm using SQL Server. The Update is the easy part. The difficult part is finding the right juxtaposition of the 2 queries to accomplish what I want. Should I use a Subquery, or maybe WHERE EXISTS? Any thoughts?
Reply With Quote
  #4 (permalink)  
Old 02-09-05, 20:04
fstop fstop is offline
Registered User
 
Join Date: Dec 2004
Posts: 22
I managed to solve the problem by using WHERE IN:

SELECT * FROM QueryA
WHERE IN
(QueryB)
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