0

我有一个 SQL Server 表。此表有两个表:Order 和 OrderInProgress。这两个表具有相似的结构:

Order[InProgress]
-----------------
ID (uniqueidentifier)
CreateDate
...

我需要获取最新的 Order 或 OrderInProgress

DECLARE @latestOrderID uniqueidentifier
@latestOrderID = ?

如何将 @latestOrderID 设置为最新的 Order 或 OrderInProgress ID?我无法弄清楚如何在 SQL 中执行此操作。

谢谢!

4

3 回答 3

1

Try this, which should be more efficient whenever it matters.

   select top 1 ID from (
        select top 1 ID from Order order by ID desc
      union all
        select top 1 ID from OrderInProgress order by ID desc
    ) T
    order by ID desc
于 2013-03-26T23:51:06.997 回答
0

你有没有尝试过:

SELECT TOP 1 @latestOrderID = ID FROM [Order] ORDER BY CreateDate DESC
于 2013-03-26T23:47:35.127 回答
0
SELECT * FROM
(SELECT 'Order' tablename, max(ID) max_id FROM Order
UNION
SELECT 'OrderInProgress' tablename, max(ID) max_id FROM OrderInProgress
) maxes ORDER BY max_id DESC
于 2013-03-26T23:47:37.343 回答