欢迎光临
我们一直在努力

用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

suppose we have a table as follows:

create table [ignore_rows] (
 [c1] [int] null ,
 [c2] [char] (10)
)

and the text file is as follows:

1,aaa
2,bbb
3,ccc
100,ddd

to ignore the first and the last row of the text file when importing the text file to the table, you can use these steps:

1. in sql enterprise manager, right click the data transformation services, click new package, this will launch the dts package designer.

2. click package –> properties menu, click the global variables tab, add two global variables:

currentrow , int, initial value 0  –> we use it to track the row we are currently processing.
lastrow, int, initial value 0         –> we use it to record the row number of the text file.

3. add an activex script task to the design pane, the script is as follows.

this script use the file system object (fso), for more information regarding fso, please check it on msdn.

function main()

dim fso
dim ts
dim rowcount

set fso = createobject(“scripting.filesystemobject”)
set ts = fso.opentextfile(“c:\data\ignore_rows.txt”, 1)  1 for reading

rowcount = 0

while not ts.atendofstream
ts.skipline
rowcount = rowcount + 1
wend

dtsglobalvariables(“currentrow”).value=0
dtsglobalvariables(“lastrow”).value=rowcount

main = dtstaskexecresult_success

end function

4. drag two connections to the pane, one text file connection and one microsoft ole db provider for sql server connection, and then drag a transform data task. the activex transformation script is as follows:

function main()

dtsglobalvariables(“currentrow”).value=dtsglobalvariables(“currentrow”).value+1

the following code will skip and first row and last row
if dtsglobalvariables(“currentrow”).value=1 or dtsglobalvariables(“currentrow”).value=dtsglobalvariables(“lastrow”).value then
 main=dtstransformstat_skiprow
else
 dtsdestination(“c1”) = dtssource(“col001”)
 dtsdestination(“c2”) = dtssource(“col002”)
 main = dtstransformstat_ok
end if

end function

5. set the precedence correctly, the final package is as follows:

activex script task –(on success)–> text file connection –(transform data)–> sql connection

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行-数据库专栏,SQL Server
分享到: 更多 (0)