sql server 2012插入排序后的数据到临时表无效

IF OBJECT_ID('TEMPDB..#list') IS NOT NULL
BEGIN
DROP TABLE TEMPDB.#list
END

CREATE TABLE #list(OFC_ID VARCHAR(40), MyNo VARCHAR(64), RowNumber INT, Flag INT)

TRUNCATE TABLE TEMPDB.#list

-- 使用ROW_NUMBER插入后有是序的(ROW_NUMBER也可用于分页)
INSERT INTO #list(OFC_ID,MyNo,RowNumber,Flag )
SELECT OFC_ID,MyNo, ROW_NUMBER() OVER ( ORDER BY OFC_ID,MyNo ) AS RowNumber ,1
FROM (
SELECT '7976B0A917A948A68A7595A907247064' AS OFC_ID, 'WHT19102401-001' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-001' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-003' AS MyNo
UNION ALL
SELECT '7976B0A917A948A68A7595A907247064' AS OFC_ID, 'WHT19102401-002' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-002' AS MyNo
) A

-- 虽然加了排序但插入到临时表中还是无序的
INSERT INTO #list(OFC_ID,MyNo,Flag )
SELECT OFC_ID,MyNo,2
FROM (
SELECT '7976B0A917A948A68A7595A907247064' AS OFC_ID, 'WHT19102401-001' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-001' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-003' AS MyNo
UNION ALL
SELECT '7976B0A917A948A68A7595A907247064' AS OFC_ID, 'WHT19102401-002' AS MyNo
UNION ALL
SELECT '01AEF3AECD694E56967A0EECCE20FEAA' AS OFC_ID, 'WHT19102402-002' AS MyNo
) A
ORDER BY OFC_ID, MyNo


SELECT * FROM #list

DROP TABLE TEMPDB.#list

posted @ 2019-10-24 14:27  唯起  阅读(534)  评论(0编辑  收藏  举报