Lawrence Parker


I am using the unpivot transformation, but I can not figure out how to use an expression in the Pivot Key Value.

The denormalized table I want to unpivot has columns like Sunday_Qty, Monday_Qty, Tuesday_Qty, etc. Just before the unpivot component, I inserted a derived column component that adds fields like DateSun, DateMon, DateTue, etc. that resolves to values like 01/07/2007, 01/08/2007, etc.

So for the various rows in the Unpivot Transformation Editor, I entered DateSun, DateMon, DateTue, etc. for the Pivot Key Value, and "EntryDate" for the pivot key value column name.

The data pipeline gets unpivoted correctly, but the rows have the literal values "DateSun", "DateMon", etc. in the EntryDate column.

How do I tell SSIS to use the DateSun column instead of the string "DateSun"

-Larry



Re: unpivot transformation and expressions

DarrenSQLIS


You cannot use an expression for that. A pattern I have used is to return the literal pivot key value and then use a following Derived Column Transformation to look at the value and return the relevent column. This allows you to combine the column values into one new colum, choosing the correct source column. So you could use something like this -

PivotKeyCol == "DateSun" [DateSun] : PivotKeyCol == "DateMon" [DateMon] : PivotKeyCol == "DateTue" [DateTue]

You may also want to consider how you are creating those columns, and could you just use a Derived Column after the un pivot and do the work there instead. I think it may be possible, but obviously I cannot see the full picture.






Re: unpivot transformation and expressions

Lawrence Parker


I think that is a reasonable approach, thanks.

BTW, I was actually doing the unpivot in my SQL using a UNION ALL after I created a temp table. But I get an error when I paste my SQL into the SSIS OLE DB Source when it has a temp table:

Error at Release Hours [OLE DB Source [3100]]: An OLE DB error has occurred. Error code: 0x80040E14.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Statement(s) could not be prepared.".
An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Invalid object name '#timesheet'.".

Why aren't temp tables allowed for this





Re: unpivot transformation and expressions

Jamie Thomson


Lawrence Parker writes:

Why aren't temp tables allowed for this

Laurence,

It isn't that they are not allowed, its because you need to know how to use them. Its a bit of a hack simply because of the very nature of temp tables being temporary.

All explained here:

Using temporary tables
http://blogs.conchango.com/jamiethomson/archive/2006/11/19/SSIS_3A00_-Using-temporary-tables.aspx

-Jamie






Re: unpivot transformation and expressions

Lawrence Parker


Thank you!





Re: unpivot transformation and expressions

Lawrence Parker


Still no luck on using temp tables. Here's a simple example using an OLE DB Source on my Data Flow Task:

CREATE TABLE #report (
report_id INT
)

insert into #report
select 1

select * from #report

drop table #report

I had to enter the SQL via the SqlCommand property b/c the component editor tries to validate the existence of the #report table, and throws an error since it doesn't exist. I also set ValidateExternalMetadata to false, set the task's DelayValidation property to false, and set the connection manager component's RetainSameConnection to true (though I don't think that would be necessary since the temp table does not need to persist outside of the OLE DB Source component).

When I execute my task, I get this error:

[MyTest 1 [1]] Error: An OLE DB error has occurred. Error code: 0x80040E14. An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Statement(s) could not be prepared.". An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Invalid object name '#report'.".

I get the same results if I use a global temp table (##report).

Any ideas

-Larry





Re: unpivot transformation and expressions

Jamie Thomson


Lawrence Parker writes:

Still no luck on using temp tables. Here's a simple example using an OLE DB Source on my Data Flow Task:

CREATE TABLE #report (
report_id INT
)

insert into #report
select 1

select * from #report

drop table #report

I had to enter the SQL via the SqlCommand property b/c the component editor tries to validate the existence of the #report table, and throws an error since it doesn't exist. I also set ValidateExternalMetadata to false, set the task's DelayValidation property to false, and set the connection manager component's RetainSameConnection to true (though I don't think that would be necessary since the temp table does not need to persist outside of the OLE DB Source component).

When I execute my task, I get this error:

[MyTest 1 [1]] Error: An OLE DB error has occurred. Error code: 0x80040E14. An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Statement(s) could not be prepared.". An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E14 Description: "Invalid object name '#report'.".

I get the same results if I use a global temp table (##report).

Any ideas

-Larry

Did you follow, to the letter, the steps that I have outlined i.e. Did you create the table outside of SSIS to begin with (step 4)

-Jamie






Re: unpivot transformation and expressions

Lawrence Parker


It's working now after creating the temp table in the Control Flow. I did create the table out of SSIS first for the purpose of metadata.

Thanks for the help.

-Larry