【问题标题】:How to import a text file with multiple layouts into SQL with SSIS如何使用 SSIS 将具有多个布局的文本文件导入 SQL
【发布时间】:2018-04-26 04:13:30
【问题描述】:

我是 SSIS 的新手,非常感谢任何帮助!

我需要导入一个逗号分隔的文本文件。 文件中的行具有不同的布局。 第一列的值指定布局。

例如:

布局 1:姓名、姓氏、年龄、身份证

布局2:身份证、工资

所以列名和数据类型完全不同。

有没有办法在不使用 SSIS 中的脚本任务的情况下导入此类文件?

【问题讨论】:

  • 我使用 SSIS 已经有一段时间了,但是您不能根据不同的列 1 将文件拆分为多个文件,添加标题然后导入它们吗?
  • 什么是SQL表结构?
  • @Hadi 有 9 种不同类型的布局。我无法分享具体细节。
  • @P.Salmon 这是我的下一个计划。想知道是否有更好的方法。

标签: sql-server ssis text-files metadata ssis-2014


【解决方案1】:

您可以使用来自 SSIS 工具箱/其他来源的平面文件源。检查https://docs.microsoft.com/en-us/sql/integration-services/connection-manager/flat-file-connection-manager了解更多信息 编辑:在你改变你的问题之后,我没有更好地理解。脚本任务是唯一的解决方案,因为您必须构建逻辑。

public override void CreateNewOutputRows()
{
    // Create the StreamReader object to read the input file
    System.IO.StreamReader reader = new System.IO.StreamReader(this.Variables.vInputFilename);

    // Loop through the file to read each line
    while (!reader.EndOfStream)
    {
        // Read one line
        string line = reader.ReadLine();

        // Break the file apart into atomic elements
        string[] items = line.Split('|');

        /*
            Each line should match one of three record types. When matched to
            the correct type, a new row in that output will be created and the
            columns from the file will be written to the appropriate output cols
        */

        // Record type 1 is Manager
        if (items[0] == "Layout 1")
        {
            OutputBuffer0.AddRow();

        }

        // Layout 2
        else if (items[0] == "Layout 2")
        {
            OutputBuffer1.AddRow();

        }
    }
}

然后根据输出连接相关表。让我知道它是否有效:)

【讨论】:

  • 我认为这不能解决我的问题,谢谢您的尝试。我需要知道如何动态更改文件布局映射
  • 这很有意义。对于更多面向 SQL 和 BI 的人,您介意告诉我如何从那里着手吗?非常感谢您的帮助!
  • 感谢大家的帮助。我请了一个具有 C# 技能的人来帮助我们编写脚本组件。文件被拆分成多个文件,然后从那里导入...
猜你喜欢
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2020-08-20
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多