执行sql脚本语句的输出顺序不遵循脚本语句的顺序

时间:2019-03-05 22:44:15

标签: sql sql-server

我有以下脚本来执行sql脚本语句,然后将输出插入到temp表中。出于某种原因,根据ORDER BY ADDEDDTTM DESC,未插入输出结果。有谁知道我该如何解决这个问题?

declare @stm nvarchar(Max), @ID int = NULL;
set @stm = 'SELECT TOP 5000 * FROM test WHERE ADDEDDTTM >= ''2019-02-01'' AND ADDEDDTTM < ''2019-03-01'' ORDER BY ADDEDDTTM DESC ';

CREATE TABLE #temp ( [ID] [int], [AddedDttm] [datetime], [MessageID] [varchar](100), [MessageParentID] [varchar](255), [UR] [varchar](20), 
    [SeqNumber] [bigint], [MsgControlID] [varchar](255), [MsgStd] [varchar](12), [MsgType] [varchar](12), [SendApp] [varchar](100), 
    [SendFacility] [varchar](100), [SendAgency] [varchar](100), [RcvApp] [varchar](100), [RcvFacility] [varchar](100),
    [Campus] [varchar](100), [ServiceName] [varchar](40), [ExecutionTime] [int], [ErrorMsg] [varchar](1000), [MsgDateTime] [datetime],
    [HopCount] [int], [MsgVer] [char](10), [ServiceVer] [char](10), [Payload] [varbinary](6000), [PatientType] [varchar](50),
    [Ward] [varchar](40), [Bed] [varchar](40))


INSERT INTO #temp execute (@stm )
    SELECT  ID as 'mhie_message_id', 
            AddedDttm as 'logged_datetime', 
            UR as 'ur', 
            MsgType as 'type', 
            Campus as 'campus', 
            ServiceName as 'service_name', 
            ErrorMsg as 'error_message'
    FROM #temp

1 个答案:

答案 0 :(得分:0)

如果未直接由ORDER BY声明,则对任何特定类型的任何订单均不提供任何保证。