我发现我能够将源表顺序保存BULK INSERT
到文件中,然后添加一个标识。
给定一个制表符分隔的表,C:\MyTable.txt
我故意将行乱序移动:
FileName FileType
wmsetup log
bar txt
wmsetup10 log
WMSysPr9 prx
WMSysPrx prx
Wudf01000Inst log
xpsp1hfm log
_default pif
0 log
002391_ tmp
005766_ tmp
我运行了以下命令并在 SQL Server 中保留了文本文件顺序:
IF EXISTS(
SELECT 1
FROM sys.tables t
INNER JOIN sys.schemas s on s.schema_id=t.schema_id
WHERE t.name='myTable'
AND t.[type]='U'
AND s.name='dbo'
)
DROP TABLE myTable
GO
CREATE TABLE dbo.myTable(FileName VARCHAR(80), FileType VARCHAR(30))
GO
BULK INSERT myTable FROM 'C:\MyTable.txt' WITH (
firstrow=2
, fieldterminator='\t'
, rowterminator='\n'
)
GO
ALTER TABLE myTable ADD ID INT IDENTITY(1,1)
GO
SELECT * FROM myTable
GO
结果:
FileName FileType ID
--------------- -------- -----------
wmsetup log 1
bar txt 2
wmsetup10 log 3
WMSysPr9 prx 4
WMSysPrx prx 5
Wudf01000Inst log 6
xpsp1hfm log 7
_default pif 8
0 log 9
002391_ tmp 10
005766_ tmp 11