MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 10th, 2003, 09:33 AM
gooky_dk gooky_dk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 7 gooky_dk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Update From

Hello.

I am a little lost... in the use of update ... from ...

Let us assume I have something like this:

UPDATE parts SET id_geometry = b.id_geometry_duplicate
FROM
parts a
INNER JOIN fn_duplicate_parts(@id_project) b ON a.id_part = b.id_part_org

How does this query know which rows it needs to update? I can see no relationsship between the tables parts and b (which by accident is also parts). I only want to update those rows in parts where id_part is in b(as id_part_org). I guess it is my lack of knowledge, but really I cannot see the logic in this.

Could anyone clarify?

Thanks in advance,

Lasse Johansen

Reply With Quote
  #2  
Old October 13th, 2003, 08:04 AM
alaistair alaistair is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 alaistair User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 37 sec
Reputation Power: 0
I have never heard of the UPDATE FROM query before, but am very glad to have stumbled on it here! I think the way it works (someone correct me if i'm wrong!) is like this:

UPDATE parts
SET id_geometry = b.id_geometry_duplicate
FROM parts a INNER JOIN atable b ON a.id_part = b.id_part_org

your joining PARTS A to ATABLE B using your ID_PART and ID_PART_ORG and are left with a reuslt set of PARTS and ATABLE. the UPDATE then uses this result set to determine what rows in PARTS gets updated with information from ATABLE.

Regards,

Alaistair

Last edited by alaistair : October 13th, 2003 at 08:37 AM.

Reply With Quote
  #3  
Old October 13th, 2003, 09:34 AM
gooky_dk gooky_dk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 7 gooky_dk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
My problem is only the last step, not the join. How can it actually see which rows in parts are to be updated?

Reply With Quote
  #4  
Old October 13th, 2003, 10:07 AM
alaistair alaistair is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 alaistair User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 37 sec
Reputation Power: 0
The last part, FROM x INNER JOIN Y, gives a result set that basically acts as a giant WHERE clause that also provides the update with the necessary criteria

Alaistair

Reply With Quote
  #5  
Old October 13th, 2003, 10:33 AM
gooky_dk gooky_dk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 7 gooky_dk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
And that is what I cannot see. How does it know which rows? It does not have any joins to make references. This is the example from Microsoft:

C. Use the UPDATE statement using information from another table
This example modifies the ytd_sales column in the titles table to reflect the most recent sales recorded in the sales table.

UPDATE titles
SET ytd_sales = titles.ytd_sales + sales.qty
FROM titles, sales
WHERE titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

Taking a look at the last part:
FROM titles, sales
WHERE titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

This is simply a JOIN which gives a result set. Where does this result set reference the table titles in:

UPDATE titles SET ytd_sales = etc etc

?????

I am lost. I guess I will become a mechanic instead of a computer programmer.

Reply With Quote
  #6  
Old October 13th, 2003, 10:37 AM
gooky_dk gooky_dk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 7 gooky_dk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What I really wanted to say with this is: If what you say is true(that it act as a big where clause), how can it know on which column to make the WHERE(the relation) based upon.... Automatically by primary key? What if the table do not have a primary key? What if - in the microsoft example - the table titles did not appear in the FROM clause, but came from something else.... This makes the explanation flood :-(

Reply With Quote
  #7  
Old November 19th, 2004, 11:07 AM
sigurthr sigurthr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 0 sigurthr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what Sql is really doing

If you have SQL Query Analyzer you enter you query and from the Query menu select estimated execution plan. this will show you exactly what the SQL engine will do to execute you query.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Update From


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway