如何在 SSIS 中展平结果集?

发布于 2024-08-11 19:29:10 字数 750 浏览 1 评论 0原文

我需要“展平”SSIS 包中的结果集 - 即获取由多行组成的结果集并生成包含连接值的单个字符串。

结果集通过存储过程返回给我,由于多种原因我无法更改该存储过程。我不想将逻辑复制到另一个存储过程中,因此限制是我需要从存储过程的结果开始并从那里开始工作。

我当前正在使用带有 ADO.NET 连接的“执行 SQL 任务”来运行存储过程,并将结果返回到对象类型的 SSIS 变量中。

我当前的想法(来自谷歌搜索等)是使用“执行脚本任务”,使用类似以下内容:

    Dim flattened As New StringBuilder()
    Dim adapter As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim row As DataRow

    adapter.Fill(dt, Dts.Variables("DataSet").Value)

    For Each row In dt.Rows
        flattened.Append(row("Column").ToString)
        flattened.Append(DELIMETER)
    Next

    Return flattened.ToString

上面的示例当前返回错误:“对象不是 ADODB.RecordSet 或 ADODB.Record。”我已经尝试了上述方法的几种不同变体来尝试解决此问题,但已经达到了我在环境中调试时遇到的挫败感。

任何想法将不胜感激! 谢谢

I have a requirement to "flatten" a resultset in an SSIS package - this is, take a result set consisting of a number of rows and produce a single string with the concatenated values.

The result set is returned to me via a stored procedure that for a number of reasons I cannot change. I do not want to duplicate the logic into another stored procedure, so the limitation is that I need to start off with the results of the stored procedure and work from there.

I am currently using an "Execute SQL Task" with an ADO.NET connection to run the stored procedure, and am returning the results into an SSIS variable of type Object.

My current thought (from googling, etc) is then to use an "Execute Script Task", using something like:

    Dim flattened As New StringBuilder()
    Dim adapter As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim row As DataRow

    adapter.Fill(dt, Dts.Variables("DataSet").Value)

    For Each row In dt.Rows
        flattened.Append(row("Column").ToString)
        flattened.Append(DELIMETER)
    Next

    Return flattened.ToString

The above example currently returns me the error: "Object is not an ADODB.RecordSet or an ADODB.Record." I have tried quite a few different variations of the above to try and resolve this issue but have reached the point where my frustrations at debugging in the environment have hit the wall.

Any thoughts would be much appreciated!
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天气好吗我好吗 2024-08-18 19:29:10

这是一个在sql中动态展平表的sql示例:

GO
CREATE TABLE CarDealer (
  DealershipID int not null,
  CarColor char(16),
  Model char(16),
  UnitsSold int
);

insert into CarDealer
    select 1, 'Yellow', 'Toyota', 5

insert into CarDealer
    select 1, 'Yellow',  'Ford', 8  

insert into CarDealer
    select 1, 'Red',  'Toyota', 3

insert into CarDealer
    select 2, 'Blue',  'Ford', 7
go

select * from CarDealer

DECLARE @PivHeaders VARCHAR(MAX)

SELECT @PivHeaders =   COALESCE(rtrim(@PivHeaders) + ',[' + rtrim(Val)+ ']',  '[' + rtrim(Val) + ']')
FROM 
(
    select distinct rtrim([VehiclesSold]) + ltrim(Color) as Val
    from
    (
        select * from CarDealer
    ) s1
    UNPIVOT
    (
        VehiclesSold For Color in
        ([CarColor], [Model])
    ) unp        
) s1

--print @PivHeaders

DECLARE @PivotSQL NVARCHAR(MAX)
SET @PivotSQL = N'select DealershipID, p.*  from 
(
    select UnitsSold, rtrim([VehiclesSold]) + ltrim(Color) as Val, DealershipID
    from
    (
        select * from CarDealer
    ) s1
    UNPIVOT
    (
        VehiclesSold For Color in
        ([CarColor], [Model])
    ) unp        
) s1
PIVOT
(   
    Sum (UnitsSold)
    FOR Val
    In 
    ('
        + @PivHeaders + '
    )
) p'

EXECUTE(@PivotSQL)

This is a sql example that dynamically flattens a table in sql:

GO
CREATE TABLE CarDealer (
  DealershipID int not null,
  CarColor char(16),
  Model char(16),
  UnitsSold int
);

insert into CarDealer
    select 1, 'Yellow', 'Toyota', 5

insert into CarDealer
    select 1, 'Yellow',  'Ford', 8  

insert into CarDealer
    select 1, 'Red',  'Toyota', 3

insert into CarDealer
    select 2, 'Blue',  'Ford', 7
go

select * from CarDealer

DECLARE @PivHeaders VARCHAR(MAX)

SELECT @PivHeaders =   COALESCE(rtrim(@PivHeaders) + ',[' + rtrim(Val)+ ']',  '[' + rtrim(Val) + ']')
FROM 
(
    select distinct rtrim([VehiclesSold]) + ltrim(Color) as Val
    from
    (
        select * from CarDealer
    ) s1
    UNPIVOT
    (
        VehiclesSold For Color in
        ([CarColor], [Model])
    ) unp        
) s1

--print @PivHeaders

DECLARE @PivotSQL NVARCHAR(MAX)
SET @PivotSQL = N'select DealershipID, p.*  from 
(
    select UnitsSold, rtrim([VehiclesSold]) + ltrim(Color) as Val, DealershipID
    from
    (
        select * from CarDealer
    ) s1
    UNPIVOT
    (
        VehiclesSold For Color in
        ([CarColor], [Model])
    ) unp        
) s1
PIVOT
(   
    Sum (UnitsSold)
    FOR Val
    In 
    ('
        + @PivHeaders + '
    )
) p'

EXECUTE(@PivotSQL)
沫尐诺 2024-08-18 19:29:10

不确定这是否是最好的方法,但我发现有效的方法是:

Dim dt As DataTable = CType(Dts.Variables("DataSet").Value, DataSet).Tables(0)

然后在循环中循环 dt 。

如果有人知道更好的方法,请仍然回答,我会相信你的答案。如果没有,那么会将这个答案留给未来遇到同样问题的人。

谢谢

Not sure if this is the best way, but something that I have found that works is:

Dim dt As DataTable = CType(Dts.Variables("DataSet").Value, DataSet).Tables(0)

And then just looping dt in my loop.

If anyone knows a better way please still answer and I will credit you the answer. If not then will leave this answer for future people with the same problem.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文