SSIS Script Component stripping underscores from column names - ssis

SSIS Script component strip's under scores from columnnames.
Example : ColumnName : Customer_ID
in Script components it looks as
public override void SourceIn_ProcessInputRow(SourceInBuffer Row)
{
Row.CustomerID
}
How can i get Column Name with underscore, as I have to pass column name to other .dll which does error logging and needs correct column name.

I came across the same problem this morning, I didn't really need the underscores so accepted it after a little complaining. Perhaps after your script task you could drop in a Derived Column transform or Copy Column transform into the dataflow and set the name to exactly what you need.

Related

Derived column Transformation Editor - I need add number at the end of order number in increment order

I have a csv file having many lines with different order number
I need to change them via SSIS Derived column Transformation Editor so I can have transformed output.
I need to write Expression that adds number at the end of order but I need different number or another order so it should be increment
Derived column Name Derived Column Expression Data Type
OrderNumber <add as new column> ?
Derived column Name Derived Column Expression Data Type
OrderNumber <add as new column> OrderNumber+"-"+"1" unicode string
I don't think you can add an incremental number using derived column transformation, you have to use a script component to achieve that.
Simply add a script component, go to Inputs and Outputs tab and add an Output column of type DT_STR. And inside the script editor use a similar script:
int intOrder = 1;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if(!Row.OrderNumber_IsNull && !String.IsNullOrEmpty(Row.OrderNumber)){
Row.outOrderNumber = Row.OrderNumber + "-" + intOrder.ToString();
intOrder++;
}else{
Row.outOrderNumber_IsNull = true;
}
}
There is no stock method to do what you are trying to achieve. What would rather be easier is to write a script component and have that generate the numbers for you. Once you get the new column out of that script component, it is easy enough to concatenate that with your existing order number.
Just curious, why cant you do this on the database itself? It would be much easier to implement and control IMO.
Here is a link to generating the numbers: Generating Surrogate keys in SSIS

Google Spreadsheet, Substitute Integer with String

Hey there and thanks in advance.
I'm exporting the API of an application onto my spreadsheet, which works fine. Due to how the API was programmed however, some of the columns now contain the TypeID (an integer representing the "name") and not the actual name. I know what TypeID represents what Name, so what I'm looking for is a way to substitute all entries of said column with the actual name.
I have already begun to make a humongus switch case in the script editor that just checks every cell in that column and based of the contents substitues the right name, but as you can probably imagine that would take a while.
Just wondering if there is a "cleaner" and more effective way.
I'd recommend making a JSON object to represent your switch case and call that
i.e :
var jsonMap = {"TYPEID":"NAME"};
Then call :
jsonMap[fieldValue]
To return the correct value for that field
You could have the script trigger on row modification and have it translate that way.
Alternatively I'd recommend mapping the field before it is exported into sheets using the language you're exporting in and have the data enter the sheet correctly

SSIS package - How to rename pivoted columns

I'm new to SSIS and just created a simple package taking input from a source file then pivot it and insert into the database. It works well for me.
I am aware that I can provide an alias name for each column under Pivot > Advance Editor > Input and Output Properties > Pivot Default Output > Output Columns > Set the "Name" property to whatever I want. I want to ask if there is away to rename the pivoted column programmatically? I have about 100 columns and thought it is more effective to do this in code but not sure how. I tried to add a script component but not able to get to the "Name" property... My end goal is to remove the "C_" from the auto generated pivot column names. This way when I'm inserting the record to the db, it can auto map for me.
Your goal "rename columns dynamically in package itself" contradicts to basic SSIS approach, which is "fix metadata, including column type and name, at design phase, and reuse at runtime". So, no script component can rename your columns.
You can use BIML or EzAPI/Microsoft SSIS DLLs to generate package based on your metadata; but once you design it, the package metadata including column names is fixed.

SSIS transactional data (different record types, one file)

An interesting one, we're evaluating ETL tools for pre-processing statement data (e.g. utility bills, bank statements) for printing.
Some of the data comes through in a single flat file, with different record types.
e.g. a record type with "01" as the first field will be address data. This will have name and address fields. A record type with "02" will be summary data, with balances and totals. Record type "03" will be a line item on the statement.
Each statement will have one 01 and 02 records, and multiple 03 records. I could pre-parse the file and split into 3 files for loading into a table, but this is less than ideal.
We take the file and do a few manipulations on it (e.g. add in a couple more fields to the address record, and maybe do some totalling / validation), and then send the file in pretty much the same format (But with the extra fields added) to our print composition program.
How would you do this in SSIS?
The big problem with variant records in SSIS is that you don't get any of the benefits of the connection manager helping with the layout, since the connection manager can only handle a single layout.
So typically, you end up with a CRLF terminated flat file with two columns: recordtype and recorddata. Then you put the conditional split in and parse each type of row on different paths. The parsing will have to split up the remaining record data and put it in columns and convert as normal, either with a derived column transform or a script transform and potentially conversion transforms.
If you had a lot of packages to do, I would seriously consider writing a custom component which produced 3 outputs already converted to your destination types.
answered my own question - see below script. AcctNum come in from a derived column from the flat file source and will be correctly populated for 02 record types, save it in local static varialbe and put it back on the row for other record types that do not contain the acct number.
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
static String AccountNumber = null;
public override void PreExecute()
{
base.PreExecute();
/*
Add your code here for preprocessing or remove if not needed
*/
}
public override void PostExecute()
{
base.PostExecute();
/*
Add your code here for postprocessing or remove if not needed
You can set read/write variables here, for example:
Variables.MyIntVar = 100
*/
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.RecordType == "02")
AccountNumber = Row.AcctNum; // Store incomming Account Number into local script variable
else if (Row.RecordType == "06" || Row.RecordType == "07" || Row.RecordType == "08" ||
Row.RecordType == "09" || Row.RecordType == "10")
Row.AcctNum = AccountNumber; // Put Stored Account Number on this row.
}
}
This is possible, bu you will have to write custom logic. I did this once with DTS.
If the file is delimited, SSIS will import the fields correctly. You can write a script that examines the record type field, then branches into different inserts depending on the record type. If the file has records that are not delimited, but each type has its own fixed widths, this becomes a lot more complicated, since you'd have to parse and split each imported line, with the record types and their width hardcoded in the script.
There are a few ways to do it, but I think the easiest one to understand would be to add a conditional split after the source task, and then push it through a bunch of data conversion tasks to get the right format of data.
Make sure that your source is set up with the correct data types, so nothing falls through (e.g.-all strings). Then just check the "Record Type" field in that conditional split to send it to the right branch.

SSIS - Is there a Data Flow Source component that will handle CSV files where the column order may change?

We have written a number of SSIS packages that import data from CSV files using the Flat File Source.
It now seems that after these packages are deployed into production, the providers of these files may deliver files where the column order of the files changes (Don't ask!). Currently if this happens, our packages will fail.
For example, an additional column is inserted at the beginning of each row. In this case, the flat file source continues to use the existing column order, which obviously has a detrimental effect on the transformation!
Eg. Using a trivial example, the original file has the following content :
OurReference,Client,Amount
235,MFI,20000.00
236,MS,30000.00
The output from the flat file source is :
OurReference Client Amount
235 ClientA 20000.00
236 ClientB 30000.00
Subsequently, the file delivered changes to :
OurReference,ClientReference,Client,Amount
235,A244,ClientA,20000.00
236,B222,ClientB,30000.00
When the existing unchanged package is run against this file, the output from the flat file source is :
OurReference Client Amount
235 A244 ClientA,20000.00
236 B222 ClientB,30000.00
Ideally, we would like to use a data source that will cope with this problem - ie which produces output based on the column names, instead of the column order.
Any suggestions would be welcomed!
Not that I know of.
A possibility to check for the problem in advance is to set up two different connection managers, one with a single flat row. This one can read the first row and tell if it's OK or not and abort.
If you want to do the work, you can take it a step further and make that flat one-field row the only connection manager, and use a script component in your flow to parse the row and assign to the columns you need later in the flow.
As far as I know, there is no way to dynamically add columns to the flow at runtime - so all the columns you need will need to be added to the script task output. Whether they can be found and get parsed from the each line is up to you. Any "new" (i.e. unanticipated) columns cannot be used. Columns which are missing you could default or throw an exception.
A final possibility is to use the SSIS object model to modify the package before running to alter the connection manager - or even to write the entire package dynamically using the object model based on an inspection of the input file. I have done quite a bit of package generation in C# using templates and then adding information based on metadata I obtained from master files describing the mainframe files.
Best approach would be to run a check before the SSIS package imports the CSV data. This may have to be an external script/application, because I don't think you can manipulate data in the MS Business Intelligence Studio.
Here is a rough approach. I will write down the limitations at the end.
Create a flat file source. Put the entire row in one column.
Do not check Column names in first data row.
Create a Script Component
Code:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string sRow = Row.Column0;
string sManipulated = string.Empty;
string temp = string.Empty;
string[] columns = sRow.Split(',');
foreach (string column in columns)
{
sManipulated = string.Format("{0}{1}", sManipulated, column.PadRight(15, ' '));
}
/* Note: For sake of demonstration I am padding to 15 chars.*/
Row.Column0 = sManipulated;
}
Create a flat file destination
Map Column0 to Column0
Limitation: I have arbitrarily padded each field to 15 characters. Points to consider:
1. Do we need to have each field of same size?
2. If yes, what is that size?
A generic way to handle that would be to create a table to store the file name, fields, and field sizes.
Use the file name to dynamically create the source and destination connection manager.
Use the field name and corresponding field size to decide the padding. Not sure, if you need this much flexibility. If you have any question, please respond.