-1

When trying to execute this SQL statement, I get error :

Syntax error (missing operator) in query expression Orders.BuyPrice" From Pro INNER JOIN Orders On Pro.ProID = Orders.ProID

Trying to Update Balance and BuyPrice in Pro Table from Orders table

dim query as string = "update Pro set Pro.Balance = Pro.Balance + Orders.Qu," & _
"Pro.BuyPrice = Orders.BuyPrice" & _
" From Pro INNER JOIN Orders On Pro.ProID = Orders.ProID " & _
"AND orders.OrderID = " & orderID

execute(query)

Execute:

execute(q as string)
connectDB
dim cmd as new ODBCCommand(q, DBcon) 
cmd.executeNonQuery()

I don't know what is wrong in query?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
E R
  • 480
  • 1
  • 8
  • 20
  • 1
    What database (DBMS) you're using? Is it SQL Server or MySQL or something else? – har07 Jun 22 '14 at 11:50
  • Possible duplicate of http://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-in-sql – Steve Jun 22 '14 at 12:06

3 Answers3

1

ODBC use MySql so try this code

update 
      Pro 
Inner join orders 
      on 
Pro.ProID = Orders.ProID
      set 
Pro.Balance = Pro.Balance + Orders.Qu,
Pro.BuyPrice = Orders.BuyPrice
      where 
orders.OrderID = orderID
0

You can not do a join within an update query. You have to do a select query first to retrieve Orders.Qu and Orders.BuyPrice and use the result in your update query which will looks like:

dim query as string = "update Pro set Pro.Balance = Pro.Balance + " & qu & ", Pro.BuyPrice = " & buy_price & ";"
Elioty
  • 81
  • 6
  • thanks for reply but in this case i must loop on orders to read Qu and price , i don't need that. i believe i can do it in one query like discussed here [link](http://stackoverflow.com/questions/2334712/update-from-select-using-sql-server/2334741#2334741) but i failed to implement it – E R Jun 22 '14 at 11:16
0

This query worked with me

Query = " update Pro Inner join orders on Pro.ProID = Orders.ProID " & _
"set Pro.Balance = Pro.Balance + Orders.Qu," & _
"Pro.BuyPrice = Orders.BuyPrice" & _
" where orders.OrderID = " & orderID
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
E R
  • 480
  • 1
  • 8
  • 20