Groups | Blog | Home
all groups > sql server (alternate) > may 2004 >

sql server (alternate) : Update query


John Winterbottom
5/7/2004 10:26:02 AM
[quoted text, click to view]

The reply was for sql server - you cross-posted to a sql server newsgroup.

In Access you can't use aggregates in a positioned update, so try something
like:

UPDATE Orders
SET amount =
DSum("itemamount", "OrderDetails", "orderno=" & [orderno])

if orderno is numeric data type, and

UPDATE Orders
SET amount =
DSum("itemaount", "OrderDetails", "orderno='" & [orderno] & "'")

if it isn't.

But as David suggested, better not to store calculated amounts at all.

David Portas
5/7/2004 2:29:10 PM
The UPDATE would look like this:

UPDATE Orders
SET amount =
(SELECT SUM(itemamount)
FROM OrderDetails
WHERE orderno = Orders.orderno)


However, don't create a redundant amount column in your Orders table if you
can avoid it. If you put the calculated column in a view instead then you
won't need to update it at all. Something like this:

CREATE VIEW Orders_With_Amounts (orderno, amount, ...)
AS
SELECT O.orderno, SUM(D.itemamount), ...
FROM Orders AS O
JOIN OrderDetails AS D
ON O.orderno = D.orderno
GROUP BY O.orderno

--
David Portas
SQL Server MVP
--

Mansoor Azam
5/7/2004 6:00:36 PM
I have 2 tables

Create Table Orders (
OrderNo Long,
Amount Double )

Create Table OrderDetails (
OrderNo Long,
Item varchar(20),
ItemAmount Double )


the sum of ItemAmount for all items in an order in OrderDetails should be
equal to the Amount in Orders table.

What is the update query to set that for all rows in Orders?



Mansoor Azam
5/7/2004 6:44:35 PM
this doesnt work in MS Access. it gives message like 'Operation must be an
updatable query' etc
any other syntax? joins?

[quoted text, click to view]

AddThis Social Bookmark Button