简体   繁体   English

SQL 2005更新查询

[英]SQL 2005 Update Query

I'm trying to write an UPDATE SQL Query to clean up my database a bit... 我正在尝试编写UPDATE SQL查询来清理我的数据库...

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... 这是我尝试过的方法,但是它确实给我带来了巨大的问题,最终没有兑现我所有的WHEREs ,这些WHEREs更改了WHEREs更改的记录...

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. 该子句不能很好地工作,它的目的是使所有记录都超过60天,这看起来不起作用,而且我不确定为什么执行代码。 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. 因此,仅OrderDate超过60天的记录应受到影响。

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... 如果您想要超过60天的记录...

and Orders.OrderDate < dateadd(day, -60, getdate())

However, since getdate() includes time, you likely want to take that out of consideration... 但是,由于getdate()包含时间,因此您可能需要考虑这一点...

and Orders.OrderDate < dateadd(day, -60, convert(char(10), getdate(), 101))

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: 另外,如果您的订单状态应该是字符串,那么它们需要用引号引起来,因此完整的查询如下所示:

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 
AND Orders.OrderDate < dateadd(day, -60, convert(char(10), getdate(), 101))

A word of advice on running updates. 关于运行更新的建议。 ALWAYS run a SELECT first with the same WHERE clause so you can identify which rows are going to be affected. 始终首先使用相同的WHERE子句运行SELECT ,以便您可以确定哪些行将受到影响。 This will save you a lot of pain. 这样可以减轻您的痛苦。 If there are too many rows that need to be updated, use SELECT TOP 5000 or something so you can at least check some of them. 如果需要更新的行太多,请使用SELECT TOP 5000 ,以便至少可以检查其中的一些。 Always know exactly what you're going to update before you update it. 在更新之前,请始终准确知道要更新的内容。

如果您希望订购日期必须在today's date and 60 days before应该执行以下操作。

WHERE start_date BETWEEN dateadd(day,-60,getdate()) AND getdate();

Use query analyzer and create a view of the orders you want to cancel. 使用查询分析器并创建要取消的订单的视图。 Views are awesome. 意见太棒了。 With views you can build the subset of data one piece at a time checking along the way that your view contains the correct subset of data. 使用视图,您可以一次查看数据的子集,以确保视图包含正确的数据子集。 For example: 例如:

Start by creating called viewOrdersToCancel that shows all Orders with a Total_Payment_Received = 0. (This is done by putting = 0 within the filter column ) 通过创建所谓viewOrdersToCancel启动,显示所有订单与Total_Payment_Received = 0(这是通过把= 0 过滤柱内完成)

Do a SELECT * FROM viewOrdersToCancel to see just those records. 执行SELECT * FROM viewOrdersToCancel ,仅查看那些记录。

Then modify the view and add the OrderDate between DateAdd(d,-60, GetDate()), GetDate()) criteria. 然后修改视图并在DateAdd(d,-60,GetDate()),GetDate()条件之间添加OrderDate

Do another SELECT * FROM viewOrdersToCancel to see just those records. 再执行一次SELECT * FROM viewOrdersToCancel ,仅查看那些记录。

Now modify the view adding the different OrderStatus values within an in clause. 现在修改视图,在in子句中添加不同的OrderStatus值。

Your view now contains the subset of Orders you want to set to "Cancelled" 现在,您的视图包含要设置为“已取消”的订单的子集

Simply run an update statement with an inner join: 只需使用内部联接运行更新语句:
( This assumes you have a unique OrderId ) 这假设您具有唯一的OrderId

UPDATE Orders  
SET OrderStatus = 'Cancelled'  
FROM Orders t1  
INNER JOIN viewOrdersToCancel t2  
ON t1.OrderId = t2.OrderId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM