Reply to Re: Inner Join Problem

Your name:

Reply:


Posted by info@bodykind.com on 12/17/06 12:53

The following query appears to work correctly in SQL server, the only
problem (and it is a major problem) is that it sets the value of the
stock field to null on every record except the one that it is reducing.

Any ideas?

----------------------------------------------------------------------------------------------------
UPDATE Products

SET Products.Stock = Products.Stock -

(
select sum(OrdersProducts.Quantity)
from OrdersProducts
where ordersproducts.ProductID=products.ProductID and
ordersproducts.orderid = 63116 group by ordersproducts.ProductID
)

FROM Products

----------------------------------------------------------------------------------------------------


Gert-Jan Strik wrote:
> The ANSI SQL compliant rewrite of this query would be:
>
> UPDATE Products
> SET Stock = (
> SELECT Products.Stock - OrdersProducts.Quantity
> FROM OrdersProducts
> WHERE OrdersProducts.ProductID = Products.ProductID
> AND OrdersProducts.OrderID=63116
> )
> WHERE EXISTS (
> SELECT *
> FROM OrdersProducts
> WHERE OrdersProducts.ProductID = Products.ProductID
> AND OrdersProducts.OrderID=63116
> )
>
> Since this is standard SQL, it will run on any SQL Server installation,
> and should run on any modern RDBMS. It might even run in MS Access.
>
> HTH,
> Gert-Jan
>
>
> "info@bodykind.com" wrote:
> >
> > The following query works fine in access and does exactly what I want
> > it to do however I get a syntax error when I port it over to SQL Server
> > 2000.
> >
> > -------------
> >
> > UPDATE OrdersProducts INNER JOIN Products ON OrdersProducts.ProductID =
> > Products.ProductID SET Products.Stock =
> > Products.Stock-OrdersProducts.Quantity
> > WHERE OrdersProducts.OrderID=63116
> >
> > --------------
> >
> > Any help would be greatly appreciated
> >
> > Many thanks

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация