following situation:
I have 2 tables on a DB2 database:
orders
order_id
order_date
customer_id
stats
order_id
status_code
By using a SELECT with JOIN I can get all of the orders with open status:
SELECT * FROM orders o LEFT JOIN stats s ON o.order_id = s.order_id
WHERE s.status_code = 'open'
In a second SQL statement I would like to update the status_code for the orders which I just selected, like so:
UPDATE stats SET status_code = 'processing' WHERE status_code = 'open'
AND order_id IN
(SELECT o.order_id
FROM orders o
LEFT JOIN stats s
ON s.order_id = o.order_id
WHERE s.status_code = 'open')
At the update statement, I got the error "cursor state not valid". Is there a workaround or better solution for this?
Important: I need exact two statements, one for SELECT and the other for UPDATE. Each statement needs to be in one single line.
Thank you.
EDIT: New version of the update statement which is unfortunately still not working:
UPDATE stats SET status_code = 'processing' WHERE status_code = 'open'
AND order_id IN
(SELECT o.order_id
FROM orders o
WHERE s.status_code = 'open' AND s.order_id = o.order_id )
I have created an example: https://sqltest.net/#1124376
My goal is to use a second UPDATE statement changing the column 'status_code' into 'processing' for the 'order_id' from the first SELECT statement.
Please note that I do not find a DB2 sandbox that's why I just used MySQL on sqltest.net as an example to illustrate my requirement.