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 multiple rows with values from another table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-30-02, 05:13
wolfram wolfram is offline
Registered User
 
Join Date: Oct 2002
Location: Washington, USA
Posts: 7
Question UPDATE multiple rows with values from another table

Apologies if this is poor etiquette; I just crossposted this to the MS Access forum.

I want to update all rows in one table with values from another. The two tables have a one-to-many relationship (D[one]-----P[many]).

As near as I can figure, the SQL needs to look like:

Code:
UPDATE D
    SET D.[Total] = 
        (SELECT SUM([Amount]) 
            FROM P 
            WHERE P.[Acct] = D.[Acct] 
                AND P.[Type] = 'PUR')
    WHERE D.[Acct] 
        IN (SELECT [Acct] FROM D)
Unfortunately, I'm trying to do this in an MS Jet DB (through Access 2000 & ADO 2.5), so I'm not sure if my SQL is wrong-headed, or if the application simply won't do what I'm trying to tell it.

As the above stands, I get an error: "Operation must use an updateable query." The help topic associated with the error suggests that the query may be attempting to update the one side of a one-to-many relationship (it is).

However, if I substitute a static value for the SET subquery (say, 0), it works fine, but obviously not as intended. In other words, this works fine:

Code:
UPDATE D
    SET D.[Total] = 0
    WHERE D.[Acct] 
        IN (SELECT [Acct] FROM D)
Does anyone have any pointers or suggestions as to what I'm doing wrong?

(Wild speculation, but could this have something to do with the type of cursor in play?)
Reply With Quote
  #2 (permalink)  
Old 11-25-02, 04:40
fadace fadace is offline
Registered User
 
Join Date: Nov 2002
Location: Switzerland
Posts: 523
Re: UPDATE multiple rows with values from another table

Code:
UPDATE D
    SET D.Total = SUM(P.Amount) 
FROM P, D 
WHERE P.Acct = D.Acct 
AND P.Type = 'PUR'
GROUP BY D.Acct
Reply With Quote
  #3 (permalink)  
Old 11-25-02, 06:52
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: UPDATE multiple rows with values from another table

Quote:
Originally posted by fadace
Code:
UPDATE D
    SET D.Total = SUM(P.Amount) 
FROM P, D 
WHERE P.Acct = D.Acct 
AND P.Type = 'PUR'
GROUP BY D.Acct
Is that valid syntax in Access? It certainly isn't in Oracle.

Also, I don't understand the following query in the original question:

UPDATE D
SET D.[Total] = 0
WHERE D.[Acct]
IN (SELECT [Acct] FROM D)

What is the purpose of the WHERE clause? It is equivalent to saying:

UPDATE D
SET D.[Total] = 0
WHERE D.[Acct] IS NOT NULL;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
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