用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行
来源:互联网 发布:淘宝怎么看同行退货率 编辑:程序博客网 时间:2024/05/05 16:39
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
- 用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行
- 用DTS导入文本文件时,怎样跳过文本文件的第一行和最后一行
- 批处理提取文本文件第一行跟最后一行的方法
- 读取一个文本文件的最后一行
- Delphi读取文本文件的最后一行
- 除去文本文件每一行的空格&&提取每一行第一个和最后一个字符串
- Java 快速读取文本文件最后一行数据
- 获取DataTabl的第一行和最后一行的数据
- vi中跳到文件的第一行和最后一行
- vi中跳到文件的第一行和最后一行
- DataTable获取第一行和最后一行的数据
- vi中跳到文件的第一行和最后一行
- vi中跳到文件的第一行和最后一行
- java快速读取文本文件最后一行数据内容
- Linux下错误:文本文件每一行最后多了 ^M
- 使用批处理提取文件夹下所有文本文件最后一行
- PHP 删除文本文件的任意一行或添加一行
- 读文本文件的每一行 取每行最后一个空格后的子串
- Rational Robot 基础使用手册(十一)
- Rational Robot 基础使用手册(十二)
- Rational Robot 基础使用手册(十三)
- WTL目录
- 改进版Community预览
- 用DTS导入文本文件时, 怎样跳过文本文件的第一行和最后一行
- 需求获取(requirement elicitation)指导方针
- 对于程序开发过程中的心态散漫的疗法
- 表单域合法性验证等问题
- Equals and HashCode in Hibernate
- 改进版Community说明
- 建议需求开发过程
- MSN / QQ 中的CRichEditCtrl (一) —— 动画表情
- java操作word,pdf的四种武器