用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行
2004-11-22 11:35
633 查看
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
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
相关文章推荐
- VC与Delphi之间动态链接库互相调用
- 用Ole Automation实现Delphi和AutoCad之间的数据交换
- Microsoft Visual Studio .NET 2003制作安装程序
- javascript表单之间的数据传递!
- PHP 的 MySQL 操作类,跟手册上的函数一样用,但是更方便了。多说无益,看了就知道了。
- oracle中的分页
- SELECT TOP N 问题
- Hibernate Tool & Files
- delphi中的时间操作技术(1)
- delphi中的时间操作技术(2)
- VB五子棋问题,请高手赐教
- IP地址的隐藏(delphi实现 )
- 解析IP地址为主机域名
- 将某一主机域名解析为IP地址
- 備份服務器端SQL SERVER數據庫至本地目錄
- 獲取SQL SERVER用戶表信息游標
- sql server7.0数据库恢复
- 有关TrueDBGrid的问题向各位大虾请教
- 一个画图类,类似ms_chart的线图,适用于科学仿真数据分析(二)
- Netscaler URL/Cookie domain Transformation 转换