的第一個字符場寬度我需要導入這種格式的文件:固定寬度的進口具有取決於線
PParent line has some fields with fixed width
CChild line of Parent Has fixed width with different widths than Parent
CAnother Child Fixed width consistent with Child-lines but not Parent
PSecond Parent has more fields again fixed width like other Parents
CThird Child This time belong to 2nd Parent as that's the preceding P-line
因此,與被固定的列是依賴於第一字符的寬度作爲P對C.我沒有想出這個文件格式,但我需要處理它的吸盤...我目前正在讀它,像這樣(簡化):
create table #fixed (
line varchar(max)
)
create table #link (
id int identity,
parent int,
linetype char,
line varchar(max)
)
bulk insert #fixed from '\\unc\path\to\file.txt'
with (
fieldterminator = ''
)
insert into #link(linetype, line)
select substring(line, 1, 1), line
from #fixed
update c set
c.parent = p.id
from #link c
cross apply (
select top 1 id from #link
where linetype = 'P' and id < c.id
order by id desc
) p
where c.linetype = 'C'
這可行,但我不喜歡它,我特別關心SQL Server插入到#link
以任意順序排列,因此在update
中丟失了正確的親子關係,特別是對於比這5行更大的文件。
但我沒有看到在這裏強制執行order
的方法,或者使用使用格式文件的bulk insert
導入此固定寬度與變化寬度格式。
編輯:一種方式我看到的是讀取該文件與openrowset(bulk '\\unc\file.txt', single_clob)
和手動提取的線條。主要是我現在的問題是,我是否應該擔心insert into #link
的這個順序,以保證轉換爲single_clob
?
您是否能夠使用像SSIS這樣的技術?您可以讀取您的平面文件,並在讀取每行時爲其分配一個連續的行號。 – Greenspark 2014-11-04 16:57:11
我僅限於這樣做,它是一塊T-SQL,可以在文件存儲和運行時進行存儲和運行通過另一個進程在文件夾中檢測到。 – funkwurm 2014-11-05 00:48:02