I’m trying to write an UPDATE SQL Query to clean up my database a bit…
Here’s what I’ve tried but it did create huge problems for me and ended up not honoring all of my WHEREs which changed records that weren’t supposed to be changed…
UPDATE Orders
SET Orders.OrderStatus = Cancelled
WHERE Orders.OrderStatus in (New,Pending,Processing,Payment Declined,Awaiting Payment,See Line Items,See Order Notes,Backordered)
AND Orders.Total_Payment_Received = 0
There was also a third clause. That clause did not work as well, it was designed to get all records older than sixty days, it looked like it wouldn’t work and I’m not sure why I executed the code. I’m afraid to post that line here cause I’ll look dumb. It was something like:
AND Orders.OrderDate BETWEEN DATEADD(Day, -60, GetDate())
So only records with OrderDate older than sixty days should be affected.
If anybody can help me compile a query that would work it’d be greatly appreciated…
I’m not completely sure if you’re saying the first update is working or not, or if it’s just the order date check that was causing problems, so I’ll just address the order date.
If you want records older than 60 days…
However, since getdate() includes time, you likely want to take that out of consideration…
The convert function in this case removes the time part of the date.
Also, if your order statuses are supposed to be strings then they need to be surrounded by quotes, so your full query would look like this:
A word of advice on running updates. ALWAYS run a
SELECTfirst with the sameWHEREclause so you can identify which rows are going to be affected. This will save you a lot of pain. If there are too many rows that need to be updated, useSELECT TOP 5000or something so you can at least check some of them. Always know exactly what you’re going to update before you update it.