Close

Results 1 to 12 of 12
  1. #1
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default SQL Server Query.....

    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


    Anyone got any ideas why this might be?

  2. #2
    DF MaSter Gazzy's Avatar
    Join Date
    Sep 2003
    Location
    Yorkshire
    Posts
    89
    Thanks
    0
    Thanked:        0
    Karma Level
    261

    Default Re: SQL Server Query.....

    Does SQL Server allow inner joins as part of an update query? (new one on me if it does).

    My first instinct would have been to write that query with the help of a subquery.

    What is the error that query engine is throwing?

    HTH,

    G

  3. #3
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: SQL Server Query.....

    Quote Originally Posted by Gazzy View Post
    Does SQL Server allow inner joins as part of an update query? (new one on me if it does).

    My first instinct would have been to write that query with the help of a subquery.

    What is the error that query engine is throwing?

    HTH,

    G
    Thanks m8, that does appear to be the issue. the error is:

    Incorrect syntax near the keyword 'INNER'

    Any suggestions on a subquery or how we might come up with a solution?

  4. #4
    DF VIP Member flipper321's Avatar
    Join Date
    Feb 2003
    Location
    Essex
    Posts
    2,696
    Thanks
    11
    Thanked:        131
    Karma Level
    475

    Default Re: SQL Server Query.....

    Try it this way....

    UPDATE OrdersProducts

    SET Products.Stock = Products.Stock-OrdersProducts.Quantity

    FROM OrdersProducts

    INNER JOIN Products ON OrdersProducts.ProductID = Products.ProductID

    WHERE OrdersProducts.OrderID=63116

  5. #5
    DF VIP Member flipper321's Avatar
    Join Date
    Feb 2003
    Location
    Essex
    Posts
    2,696
    Thanks
    11
    Thanked:        131
    Karma Level
    475

    Default Re: SQL Server Query.....

    In fact - if you are trying to update the Products table it should be more like...

    UPDATE Products

    SET Products.Stock = Products.Stock-OrdersProducts.Quantity

    FROM Products

    INNER JOIN OrdersProducts ON OrdersProducts.ProductID = Products.ProductID

    WHERE OrdersProducts.OrderID=63116

  6. #6
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: SQL Server Query.....

    Thanks Flipper, but I've already tried that.

    It doesn't work properly when there are multiple ordersproducts records with the same productID - it only takes the first one and doesn't sum them up.

  7. #7
    DF VIP Member flipper321's Avatar
    Join Date
    Feb 2003
    Location
    Essex
    Posts
    2,696
    Thanks
    11
    Thanked:        131
    Karma Level
    475

    Default Re: SQL Server Query.....

    Oh, you want them summed... that's a very different query...

    Give me a minute...

  8. #8
    DF VIP Member flipper321's Avatar
    Join Date
    Feb 2003
    Location
    Essex
    Posts
    2,696
    Thanks
    11
    Thanked:        131
    Karma Level
    475

    Default Re: SQL Server Query.....

    UPDATE Products

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

    FROM Products

  9. #9
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: SQL Server Query.....

    Thanks m8, but that hasn't worked, get the following error:

    Incorrect syntax near the keyword 'where'.

    And it set every Products.Stock field to Null

  10. #10
    DF MaSter Gazzy's Avatar
    Join Date
    Sep 2003
    Location
    Yorkshire
    Posts
    89
    Thanks
    0
    Thanked:        0
    Karma Level
    261

    Default Re: SQL Server Query.....

    Hi Czar,

    If you can PM me a rough schema for your SQL database in script form, and a few rows of data, I'll rasp something out for you.

    Cheers,

    G

  11. #11
    DF VIP Member mysterym's Avatar
    Join Date
    May 2002
    Location
    90210
    Posts
    1,615
    Thanks
    70
    Thanked:        59
    Karma Level
    346

    Default Re: SQL Server Query.....

    Quote Originally Posted by flipper321 View Post
    UPDATE Products

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

    FROM Products
    lol the group by and nested where clause have to be switched around:

    Select ...
    Where...
    Group By..
    Having..

    Thats just off the top of my head.

  12. #12
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: SQL Server Query.....

    Got it sorted over the weekend, thanks for your replies.

Similar Threads

  1. Madden server cracks
    By articdomain in forum PC Gaming
    Replies: 0
    Last Post: 24th September 2002, 04:20 AM
  2. Help! with mail server
    By qui~Gon in forum PC Software
    Replies: 4
    Last Post: 9th September 2002, 12:37 PM
  3. AR2 v2 Query
    By rmj2663 in forum Sony Consoles
    Replies: 6
    Last Post: 8th September 2002, 07:54 PM
  4. Setting up a web server at home...?
    By bugnote in forum Web Hosting & Domain Names
    Replies: 4
    Last Post: 30th August 2002, 11:21 AM
  5. NTL's POP3 e-mail server
    By {film_man} in forum Internet Connections & VPNs
    Replies: 2
    Last Post: 29th August 2002, 11:50 AM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •