加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 如何将具有相同名称和模式但不同目录的文本文件导入数据

发布时间:2020-12-12 07:01:32 所属栏目:MsSql教程 来源:网络整理
导读:我需要将具有相同名称和相同模式的多个txt文件导入SQL Server 2008数据库中的同一个表中.我遇到的问题是它们都在不同的目录中: TEST 201304 sample1.txt sample2.txt 201305 sample1.txt sample2.txt 201306 sample1.txt sample2.txt 在SSIS中我有什么方法可
我需要将具有相同名称和相同模式的多个txt文件导入SQL Server 2008数据库中的同一个表中.我遇到的问题是它们都在不同的目录中:
TEST
     201304
            sample1.txt
            sample2.txt
     201305
            sample1.txt
            sample2.txt
     201306
            sample1.txt
            sample2.txt

在SSIS中我有什么方法可以设置它吗?

解决方法

是.您将需要使用 Foreach File Container,然后检查Traverse子文件夹选项.

编辑

显然我的回答并不充分,所以请接受这个工作代码,说明我简短的原始答案.

来源数据

我创建了3个文件夹,如上所述,包含文件sample1.txt和sample2.txt

C:&;MKDIR SSISDATASOTEST201304
C:&;MKDIR SSISDATASOTEST201305
C:&;MKDIR SSISDATASOTEST201306

该文件的内容如下.每个文件夹中的每个文件版本都会增加ID值以及更改的文本值,以证明它已经拾取了新文件.

ID,value
1,ABC

包生成

本部分假设您已安装BIDS Helper.它不是解决方案所必需的,而是简单地提供了未来读者可以用来重现此解决方案的通用框架

我创建了一个包含以下内容的BIML文件.即使我有表创建步骤,我需要在生成包之前在目标服务器上运行.

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <!-- Create a basic flat file source definition -->
    <FileFormats>
        <FlatFileFormat
            Name="FFFSrc"
            CodePage="1252"
            RowDelimiter="CRLF"
            IsUnicode="false"
            FlatFileType="Delimited"
            ColumnNamesInFirstDataRow="true"
        >
            <Columns>
                <Column
                    Name="ID"
                    DataType="Int32"
                    Delimiter=","
                    ColumnType="Delimited"
                />
                <Column
                    Name="value"
                    DataType="AnsiString"
                    Delimiter="CRLF"
                    InputLength="20"
                    MaximumWidth="20"
                    Length="20"
                    CodePage="1252"
                    ColumnType="Delimited"
                    />
            </Columns>
        </FlatFileFormat>
    </FileFormats>

    <!-- Create a connection that uses the flat file format defined above-->
    <Connections>
        <FlatFileConnection
            Name="FFSrc"
            FileFormat="FFFSrc"
            FilePath="C:ssisdatasoTEST201306sample1.txt"
            DelayValidation="true"
        />
        <OleDbConnection
            Name="tempdb"
            ConnectionString="Data Source=localhostdev2012;Initial Catalog=tempdb;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;"
        />

    </Connections>

    <!-- Create a package to illustrate how to apply an expression on the Connection Manager -->
    <Packages>
        <Package
            Name="so_19957451"
            ConstraintMode="Linear"
        >
            <Connections>
                <Connection ConnectionName="tempdb"/>
                <Connection ConnectionName="FFSrc">
                    <Expressions>
                        <!-- Assign a variable to the ConnectionString property. 
                        The syntax for this is ConnectionManagerName.Property -->
                        <Expression PropertyName="FFSrc.ConnectionString">@[User::CurrentFileName]</Expression>
                    </Expressions>
                </Connection>
            </Connections>

            <!-- Create a single variable that points to the current file -->
            <Variables>
                <Variable Name="CurrentFileName" DataType="String">C:ssisdatasoTEST201306sample1.txt</Variable>
                <Variable Name="FileMask" DataType="String">*.txt</Variable>
                <Variable Name="SourceFolder" DataType="String">C:ssisdatasoTEST</Variable>
                <Variable Name="RowCountInput" DataType="Int32">0</Variable>
                <Variable Name="TargetTable" DataType="String">[dbo].[so_19957451]</Variable>
            </Variables>

            <!-- Add a foreach file enumerator. Use the above -->
            <Tasks>
                <ExecuteSQL 
                    Name="SQL Create Table"
                    ConnectionName="tempdb">
                    <DirectInput>
                        IF NOT EXISTS (SELECT * FROM sys.tables T WHERE T.name = 'so_19957451' and T.schema_id = schema_id('dbo'))
                        BEGIN
                            CREATE TABLE dbo.so_19957451(ID int NOT NULL,value varchar(20) NOT NULL);
                        END
                    </DirectInput>
                </ExecuteSQL>
                <ForEachFileLoop
                    Name="FELC Consume files"
                    FileSpecification="*.csv"
                    ProcessSubfolders="true"
                    RetrieveFileNameFormat="FullyQualified"
                    Folder="C:"
                    ConstraintMode="Linear"
                >
                    <!-- Define the expressions to make the input folder and the file mask 
                    driven by variable values -->
                    <Expressions>
                        <Expression PropertyName="Directory">@[User::SourceFolder]</Expression>
                        <Expression PropertyName="FileSpec">@[User::FileMask]</Expression>
                    </Expressions>
                    <VariableMappings>
                        <!-- Notice that we use the convention of User.Variable name here -->
                        <VariableMapping
                            Name="0"
                            VariableName="User.CurrentFileName"
                        />
                    </VariableMappings>
                    <Tasks>
                        <Dataflow Name="DFT Import file" DelayValidation="true">
                            <Transformations>
                                <FlatFileSource Name="FFS Sample" ConnectionName="FFSrc"/>
                                <RowCount Name="RC Source" VariableName="User.RowCountInput"/>
                                <OleDbDestination 
                                    Name="OLE_DST"
                                    ConnectionName="tempdb">
                                    <TableFromVariableOutput VariableName="User.TargetTable"/>                                  
                                </OleDbDestination>
                            </Transformations>
                        </Dataflow>
                    </Tasks>
                </ForEachFileLoop>
            </Tasks>
        </Package>
    </Packages>
</Biml>

右键单击biml文件,然后选择Generate SSIS Package.此时,您应该将一个名为so_19957451的包添加到当前的SSIS项目中.

包配置

没有任何配置,因为它已经通过BIML完成,但是moar截图可以提供更好的答案.

这是基本包

这是我的变量

配置Foreach循环,如MSDN文章中所述,以及我选择Traverse子文件夹的注释

将每个循环生成的值分配给变量Current

平面文件源具有应用于ConnectionString属性的表达式,以确保它使用Variable @User :: CurrentFileName.这会更改每次执行循环的源.

执行结果

数据库的结果

匹配包执行的输出

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201304sample1.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201304sample1.txt” has ended.

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201304sample2.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201304sample2.txt” has ended.

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201305sample1.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201305sample1.txt” has ended.

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201305sample2.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201305sample2.txt” has ended.

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201306sample1.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201306sample1.txt” has ended.

Information: 0x402090DC at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201306sample2.txt” has started.

Information: 0x402090DD at DFT Import file,FFS Sample [2]: The processing of file “C:ssisdatasoTEST201306sample2.txt” has ended.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读