sql - 在SSIS中将带有逗号分隔符的单列数据拆分为多列

标签 sql sql-server csv ssis split

我在 SQL Server 中有一个包含 3 列的表,其中一列是数据列,其中包含由逗号分隔的串联列行。第一行也是我要创建的新表的标题行。所以基本上我想改变这个。

Data      | ID | Source 
====================
a,b,c,d,e | 1  | a.csv

f,g,h,i,j | 2  | b.csv

进入

a | b | c | d | e
=================
f | g | h | i | j

使用SSIS,我能想到的唯一方法是使用转储到数据列的文本文件中,然后将其作为平面文件源重新读取,但我宁愿避免创建额外的不必要的文件

编辑:抱歉我使用 SSIS 2008

最佳答案

您可以做的就是按原样读取文件。 并在脚本任务中拆分这些值。

因此从源代码转到脚本任务。 然后在作为输入列的脚本任务中,选择包含这些值的列 (InputColumn1)。然后指定输出列(如果我是对的,我看到您有 5 个列,因此指定 5 (OutputColumn1 - 5))。

完成后,转到脚本本身 (C#)。

下:

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
}

将以下代码放入其中:

var ColumnValue = Row.InputColumn1.Split(',');

Row.OutputColumn1 = ColumnValue[0];
Row.OutputColumn2 = ColumnValue[1];
Row.OutputColumn3 = ColumnValue[2];
Row.OutputColumn4 = ColumnValue[3];
Row.OutputColumn5 = ColumnValue[4];

完成脚本任务后,源中的所有列以及 OutputCoulmns1-5 将可用,您可以执行必须执行的操作。

输出

 Data      | ID | Source |OutputColumn1 |OutputColumn2|  etc. 3-5
 ================================================================
 a,b,c,d,e | 1  | a.csv  |  a           |  b

 f,g,h,i,j | 2  | b.csv  |  f           |  g

有什么不清楚的地方请追问。

关于sql - 在SSIS中将带有逗号分隔符的单列数据拆分为多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19653995/

相关文章:

php - fatal error : Uncaught exception 'PDOException' with 2 queries

sql - Oracle SQL - 从具有重复节点的 XML 中提取 clob 值

sql - 存储过程 NULL 参数

sql - 将 Sum 添加到 Select 中的 Listagg 的每一行

sql-server - 如何更改 SQL Server 中列的数据类型?

android - 如何在CSV文件中转义逗号,android

javascript - 如何将给定 excel 电子表格中的美元值正确存储到 postgresql 数据库中

sql - 'taking it offline' 进程失败后数据库无法访问

sql - 高级 SQL Server 订购方式

phpmyadmin 导出到 csv for excel