问题
I am importing a flat file with the following format:
H(tab)OrderNumber(tab)CustomerNumber(tab)ERPMessage
D(tab)OrderNumber(tab)ItemNumber(tab)ItemDescription(tab)ItemPrice(tab)Qty
D(tab)OrderNumber(tab)ItemNumber(tab)ItemDescription(tab)ItemPrice(tab)Qty
.
.
.
I am BULK LOADing the file using a format file to a staging table that looks like this:
RecordType varchar(1)
RecordDetail varchar(MAX)
so when it hits my staging table, it looks like this:
RecordType | RecordDetail
----------------------------------------------------------
H | OrderNumber(tab)CustomerNumber(tab)ERPMessage
D | OrderNumber(tab)ItemNumber(tab)ItemDescription(tab)ItemPrice(tab)Qty
D | OrderNumber(tab)ItemNumber(tab)ItemDescription(tab)ItemPrice(tab)Qty
In my control flow, I set a variable (strSubfolder) based on the name of the subfolder the file is loaded from in my ForEach loop (ie: Sub_1, Sub_2, etc).
In my data task, I read the staging table and peform a conditional split based on the RecordType, and creating derived columns based on the strSubfolder variable. What I need to be able to do is parse The RecordDetail field into its respective Header (H) and Detail(D) tables, and include the strSubfolder as a derived column to each table:
[Header table]
OrderNumber | SubFolder | CustomerNumber | ERPMessage
-----------------------------------------------------
[Detail table]
OrderNumber | SubFolder | ItemNumber | ItemDescription | ItemPrice | Qty
------------------------------------------------------------------------
How do I parse the RecordDetail field of my staging table, essentially treating it like its own BULK INSERT? Am I going about this in the entirely wrong way?
回答1:
I suggest using derived columns to populate these 7 columns, you can use TOKEN()
function with SSIS conditional operators ? :
:
OrderNumber
TOKEN([RecordDetail],"\t",1)
CustomerNumber
[RecordType] == "H" ? TOKEN([RecordDetail],"\t",2) : NULL(DT_WSTR,50)
ERPMessage
[RecordType] == "H" ? TOKEN([RecordDetail],"\t",3) : NULL(DT_WSTR,50)
ItemNumber
[RecordType] == "D" ? TOKEN([RecordDetail],"\t",2) : NULL(DT_WSTR,50)
ItemDescription
[RecordType] == "D" ? TOKEN([RecordDetail],"\t",3) : NULL(DT_WSTR,50)
ItemPrice
[RecordType] == "D" ? TOKEN([RecordDetail],"\t",4) : NULL(DT_WSTR,50)
Qty
[RecordType] == "D" ? TOKEN([RecordDetail],"\t",5) : NULL(DT_WSTR,50)
来源:https://stackoverflow.com/questions/57435110/treating-a-tab-delimted-column-as-a-bulk-insert-in-ssis