I have a SQL Server database and I need to manually execute the update request. There are no solutions using any programming language (stored procedures can be used)
I have 4 tables affected (/ used) in a query.
- [Orders]
- [StatusHistoryForOrder]
- [StatusHistory]
- [Statuses]
I need to update the [Orders] field. [OrderStatusID], which is the foreign key for [Statuses]. (So, actually changing the state of the order. The [StatusHistoryForOrder] table is the middleware table in the [StatusHistory] and contains only 2 columns.
- [StatusHistoryForOrder]. [OrderId]
- [StatusHistoryForOrder]. [OrderStatusHistoryid]
Do not say that this is not logical, because I already know this. The company that developed the database is a completely backward company, but the database is now too large to install everything in a row, and there is no time and money for this.
The [StatusHistory] table has several columns:
- [StatusHistory]. [OrderStatusHistoryId]
- [StatusHistory]. [OrderStatusId]
- [StatusHistory]. [The date]
- [StatusHistory]. [Message]
[StatusHistory]. [OrderStatusId] is also a foreign key for [Statuses].
In the update request, I need to update the order status to status 16. But only in the lines that now have the status of 1 and older than 60 days. I know that I can check the date using the function
DATEDIFF(DD,[StatusHistory].[Date],GETDATE()) > 60
, . [StatusHistory], , [StatusHistoryForOrder] , [].
- , ? SQL Server ( SQL, ), , .
:
, [], [StatusHistory]. [] ( ) 60. , StatusHistory 16. [StatusHistoryForOrder] statusHistory, [StatusHistoryForOrder]. [OrderStatusHistoryid] , [ StatusHistoryForOrder]. [OrderId]. , : []. [OrderStatusID] 16.
, :
SELECT TOP (100) PERCENT
dbo.Orders.OrderID,
dbo.Statuses.Description AS Status,
dbo.StatusHistory.Date
FROM
dbo.Orders
INNER JOIN
dbo.Statuses
ON
dbo.Orders.OrderStatusID = dbo.Statuses.StatusId
INNER JOIN
dbo.StatusHistoryForOrder
ON
dbo.Orders.OrderID = dbo.StatusHistoryForOrder.OrderId
INNER JOIN
dbo.StatusHistory
ON
dbo.StatusHistoryForOrder.OrderStatusHistoryid = dbo.StatusHistory.OrderStatusHistoryId
WHERE
(dbo.Statuses.StatusId = 1)
AND
(DATEDIFF(DD, dbo.StatusHistory.Date, GETDATE()) > 60)
UPDATE
@marc_s:

- ?