此查询始终返回相同数量的行,但每次都以不同的顺序返回。为什么会这样?
我需要添加更多过滤器,但我无法完成此步骤。
BEGIN
DECLARE @lastStatus Varchar(10)
SELECT
[Job].[Job],
[Job].[Part_Number],
[Job].[Rev],
[Job_Operation].[Description],
[Job].[Customer_PO],
[Job].[Customer_PO_LN],
[Delivery].[Promised_Date],
[Job_Operation].[Operation_Service],
[Job].[Note_Text],
[Job_Operation].[Status],
[Job_Operation].[Sequence]
INTO [#tmpTbl]
FROM [PRODUCTION].[dbo].[Job_Operation]
INNER JOIN [Job]
ON [Job_Operation].[Job]=[Job].[Job]
INNER JOIN [Delivery]
ON [Job_Operation].[Job]=[Delivery].[Job]
WHERE [Job].[Status]='Complete'
ORDER BY [Job_Operation].[Job],[Job_Operation].[Sequence]
SELECT *
FROM [#tmpTbl]
DROP TABLE [#tmpTbl]
END
答案 0 :(得分:8)
将Order By
放在Select * From #tmpTbl
上,而不是放在insert
上。
答案 1 :(得分:1)
SELECT j.Job,
,j.[Part_Number]
,j.Rev
,j_O.Description
,j.Customer_PO
,j.[Customer_PO_LN]
,d.[Promised_Date]
,j_o.[Operation_Service]
,j.[Note_Text],
,j_o.Status,
,j_o.Sequence
,j.[Customer_PO],
,j.[Customer_PO_LN],
,d.[Promised_Date],
,j_o.[Operation_Service],
,j.[Note_Text],
,j_o.[Status],
[Job_Operation].[Sequence]
INTO [#tmpTbl]
FROM [PRODUCTION].[dbo].[Job_Operation] j_o
INNER JOIN Job j
ON j_o.Job = j.Job
INNER JOIN Delivery d
ON j_o.Job= d.Job
WHERE j.Status='Complete'
ORDER BY j_o.Job,j_o.Sequence
SELECT *
FROM [#tmpTbl]
DROP TABLE [#tmpTbl]
END
答案 2 :(得分:0)
因为从#tmpTbl
中选择时没有order by子句尝试
SELECT *
FROM [#tmpTbl]
ORDER BY Job, Sequence
答案 3 :(得分:0)
您无法通过SET命令(即SELECT INTO)指定订单数据进入表中 - 这取决于表是否在创建后定义了聚簇索引。
当您最终从该表中选择以获得结果时,您可以控制数据的顺序。
SELECT * FROM [#tmpTbl] ORDER BY ....