Wed 4 Apr 2007
SSIS – Using a Script Component as a Source
Posted by Phil Brammer under SQL Server 2005 , SQL Server 2008 , SSIS Data flow[39] Comments
Just a quick tutorial on using a script component as a source… Follow the link for screenshots.
First things first. Add a script component to the data flow. When prompted, select “Source” as the component type. (click on images for full sized version)
Next, for clarity’s sake, I’ll rename the default output. (An output simply contains a set of columns and will correlate to the green arrow on the data flow. The more outputs you have, the more green arrows you can use.) I’ll rename the default output, “Output 0″ to “MyOutput.”
Next, I’ll add an output column called, “MyColumn” and its type will be an integer. Be sure to change the type accordingly.
From here, we can edit the script by clicking on the script section on the left of the above window. From there click on the “Design Script…” button. Below is the setup:
Notice how when I type, I am automatically prompted for a list of available items to choose from. One of them is the column I added previously. Let’s pick the column and assign a value of 123 to it. Full script below:
For SSIS 2005:
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
MyOutputBuffer.AddRow()
MyOutputBuffer.MyColumn = 123
End Sub
End Class
For SSIS 2008, it’s a bit different:
Visual Basic:
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute> _
<CLSCompliant(False)> _
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
Output0Buffer.AddRow()
Output0Buffer.Column = 123
End Sub
End Class
C#:
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
{
public override void CreateNewOutputRows()
{
Output0Buffer.AddRow();
Output0Buffer.Column = 123;
}
}
You can close the script and then click OK on the script component dialog box. Now when you hook this source up to another component, you'll end up with one column with one row with a value of 123.




April 17th, 2007 at 7:41 am
And a script component with input/output as a transformation?!
Regards,
pedro
April 23rd, 2007 at 3:26 pm
@pedro: ‘Asynchronous transformation’ is what you’re looking for. Check the BOL.
April 25th, 2007 at 10:20 am
Is there a way to script the creation of the columns?
August 7th, 2007 at 8:05 am
OK.. this works fine for a predefined source. How about wanting to make a single source component that can manipulate different sources, translate them into different output sources?
ie. loop through list of tables and transfer to corresponding output tables. This is easily enough done if your tables have the same structure, you can just replace the tablename/sqlsource using expressions, but when you run into different table structures METADATA destroys LOL…
August 7th, 2007 at 8:09 am
Woody,
That’s correct. You can try the Transfer SQL Server Objects to do what you desire, provided you are working with SQL Server. Otherwise, if the metadata is different, you can’t expect a data flow to be able to handle that as metadata is fixed at design time. I do not want it to be able to handle changing metadata on the fly as that just creates data quality issues.
December 25th, 2007 at 7:40 am
Any solution when work on SSIS with Oracle for “column cross reference”? Thanks
December 26th, 2007 at 9:56 am
Thomas,
I’m not sure what you mean by “column cross reference.”
February 5th, 2008 at 7:45 am
hey,
how abt if we a package variable, that we need to assign to the script column instead of 123 in your example
February 5th, 2008 at 8:30 am
gautam beri,
On the Script tab of the editor, you can add your package variable to the property, ReadOnlyVariables. CaSE matters, and do not include any extra spaces.
Then, inside your script, you can create a VB.Net variable (that will hold your SSIS package variable) and then in the assignment, you can reference the package variable like this:
myVBLocalVariable = Me.MySSISPackageVariable
April 10th, 2008 at 12:01 pm
Hi there,
How can I use a script component like yours or a script task to built a listbox(based on a column of my table) and prompt user to select values from the list ?
So far, i’ve been able to prompt user using an inputbox like this:
Dts.Variables(“PromptUserVal”).Value = InputBox(“Enter Code please”, “Prompt”)
I know this might not be the place to ask this but i thought you might help me…
Tks so much in advance!
April 10th, 2008 at 12:07 pm
Liliane,
SSIS is not an end user tool and should not be used as such. For this, why are you using SSIS at all? Why not write a VB app to do this?
For every user that you want to have use this solution of yours, a SQL Server license will be required for that machine.
April 10th, 2008 at 12:45 pm
Hi Phil,
Thanks so much for you quick answer. really appreciated.
we’re trying to load new Data and we were trying to do so using SSIS… and we need the user to configure one of the fields we need…
Liliane
April 10th, 2008 at 12:47 pm
just to answer your question, yes every user have a SQL server license. but i guess i should just use a VB app like you suggested.
thanks a lot once again…
Liliane
April 11th, 2008 at 1:41 pm
I am trying to assign value to package variable in a script. It tells me that element can not be found in collection. What I am doing wrong in this code:
Dts.Variables(“DesinationDirectory”).Value = “C:\”
DestinationDirectory is listed in Script Editor window as ReadWrite variable.
Thanks
Lenny
April 11th, 2008 at 1:57 pm
Just worked when I designated variable as ReadOnly. Does not make sense to me.
April 11th, 2008 at 2:05 pm
Lenny,
You can only write to variables in a Script Component in the PostExecute method…
Another method of notation would be to use “Me.Variables.DestinationDirectory” to reference the variable listed in ReadOnly or ReadWrite Variables.
April 11th, 2008 at 7:44 pm
Sorry what is PostExecute method. Also “Me.” notation did not gave me Variables option in IntelliSense
Thanks you for guiding me to the right path
April 11th, 2008 at 9:44 pm
If “Me.” didn’t pop anything related to “Variables” up, then I have to suspect that you are in the Script Task, not the Script Component.
In the Script Task (which is on the Control Flow), you can do two things. Use the ReadOnlyVariables or ReadWriteVariables properties of the Script Task. List your variables here. Note that they are case sensitive and multiples should be separated by a comma (,) and NO spaces.
If you use the ReadOnly/ReadWrite properties, you can simply place the following in an empty Script Task: (setup an SSIS string variable, “test123″, set it to be a string, and give it some default value)
MsgBox(Dts.Variables(“test123″).Value.ToString)
Dts.Variables(“test123″).Value = “This is after updating.”
MsgBox(Dts.Variables(“test123″).Value.ToString)
If you don’t want to use the ReadOnly/ReadWrite properties (my preference), you’ll have to do this:
Dim vars As Variables
Dts.VariableDispenser.LockForWrite(“test123″)
Dts.VariableDispenser.GetVariables(vars)
vars(“test123″).Value = “This is after updating”
MsgBox(vars(“test123″).Value.ToString)
vars.Unlock()
In a Script Component, things are a bit different, and to write to a variable, you need to use PostExecute():
http://oassaf.wordpress.com/2008/01/31/writing-to-an-ssis-variable-within-a-dataflow-script-component/
Or here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=97494&SiteID=1
July 24th, 2008 at 2:52 pm
Hi, I’ve a question,
why can’t I set an output Column with a value? It show me that the output column it’s read only.
Excellent post.
Thanks.
August 6th, 2008 at 2:24 am
Hi,
I want to import a flat file into a table. Before this, I have to look up a sequence number in another table, increase it’s value and add this number as a unique id to this row. I tried to do this with a synchronous and asynchronous transformation but couldn’make it work, because in the synchronous mode I don’t have the opportunity of CreateNewOutputRows() in the asynchronous mode the values of the flat file get lost during transformation.
Does anybody has another suggestion, how to mananage this task?
Thanks in advance, Alexander
August 6th, 2008 at 6:17 am
Alexander,
Have you looked at my post on surrogate keys? This does what you want to do, I believe: http://www.ssistalk.com/2007/02/20/generating-surrogate-keys/
January 21st, 2009 at 6:53 am
Hello Phil,
I have to read attachments from some specific mail id from an outlook exchange server and push the data from the attachments to a database table. I read that it can be done using SSIS. I am new to SSIS, Can you please guide me through the steps? I would be very grateful.
Thanks in advance.
-Nidhi
March 5th, 2009 at 10:18 am
Thanx for the post, this helped
Answer for:
Is there a way to script the creation of the columns?
Yes you can.. It takes some work but what you need to do is actually script it out in a loop into a textbox control so you can split the lines and copy it into the dtsx file genereated by the designer.
This beats creating hundreds of columns for several tables!!
just create a loop and set increase your ID number by 1..
March 6th, 2009 at 4:18 pm
I still can’t pass variable instead of 123
Please help.
March 6th, 2009 at 4:45 pm
I get the file name from the foreach File enumarator before moving the file to different location, I need to check if its name matches with one of the filename in the table. I tried to use script as source and then lookup but now what is next. I have a script that moves file in different folder after that. but how to get filtered files?
Please help.
Thanks,
January 28th, 2010 at 3:13 pm
I have the same issue as Hugh above. My question is if this is not tricky in 2008. I have done exactly how it is defined above and I get a problem writing a value to the column because it is read only. I see nowhere that I can change the properties so that the column is writable. Please any help is greatly appreciated.
January 28th, 2010 at 3:48 pm
Using2008,
I’ve posted some updated code for use in SSIS 2008. Please try that. If you have the Script Component set to act as a source, you should be fine. I’ve just tested the code above and it works correctly.
February 11th, 2010 at 8:57 am
Hello,
This one is pretty clear, but I’ve a question regarding creation of dynamic OutputColumns with Output0Buffer.
Could you please tell me how can I create dynamic columns (for example:RowID and RowName) using Output0Buffer in Script Component with C# or vb.net
[ like
Output0Buffer new_column= Output0Buffer.New();
new_column.Name="RowName_YYYYMM" // YYYYMM depends on date which should be dynamic
]
Bcoz my columns names are to be varied often which i get from my client (column name with YYYYMM as suffix).
i cannot change the column names of output buffer every month..
Thanks in advance
Santosh Kumar
March 2nd, 2010 at 4:03 pm
One question? I have created a SSIS package and used input box in the script. The purpose of input box is to provide source file name which needs to loaded. When I am running the package it works perfect. However, when I put the package in the job I don’t get prompted for file name. The reason for such setup is this our helpdesk should able to run the job by themselves using the job as they have assigned to SQLServerAgent Operator. We don’t want them to connect to integration services and run the package from there.
Any help?
March 9th, 2010 at 10:56 pm
Hi Athar,
What you need to do is remove the msgbox from the package. and make use of SSIS Configuration. Once it is set up the people running the job just need to modify the config file and the package would work the same way.
July 14th, 2010 at 9:45 am
Is there a way that a .Net DataTable can be assigned directly to a data buffer in the script. Although I see that the BufferWrapper class doesn’t have any such method.
Something like,
for each(dt as DataTable in myDataSet.Tables)
TestSSISInsertBuffer = dt
Next
Adding data row by row to the buffer would degrade performance, I believe. Or am I missing an alternative that can map my data table data directly to database?
Thanks.
September 7th, 2010 at 9:40 pm
Need to write VBscript in script task in SSIS to check the data for DATE and previous dates not downloaded..
Help Appreciated…..
September 20th, 2010 at 10:37 am
Please help me here!!
I have this URL where there is XML data. I have to extract that data from URL and dump it into DW table. I am using SSIS Script Task for that. OR is it better to use Script Component as Data SOurce in DFT?
I am not good with Scripting.
This is how the data looks like:
-
AED
United Arab Emirates Dirhams
3.6732001305
0.2722421770
−
AFN
Afghanistan Afghanis
44.0000000000
0.0227272727
−
ALL
Albania Leke
104.4100000001
0.0095776267
This is the code i’m using to load it into some Object type or something.
public void Main()
{
String URLString = “http://www.xe.com/dfs/datafeed2.cgi?beeline”;
XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader(URLString);
doc.Load(reader);
XmlNodeList currencynodes = doc.SelectNodes(“currency”);
foreach(XmlNode currency in currencynodes)
{
XmlNode csymbol = currency.SelectSingleNode(“csymbol”);
string csymbolvalue = csymbol.Value;
XmlNode cname = currency.SelectSingleNode(“cname”);
string cnamevalue = cname.Value;
XmlNode crate = currency.SelectSingleNode(“crate”);
string cratevalue = crate.Value;
XmlNode cinverse = currency.SelectSingleNode(“cinverse”);
string cinversevalue = cinverse.Value;
Dts.Variables["User::oCurrencyConversion"].Value = csymbol.Value;
}
Help appreciated. Thanks
January 17th, 2011 at 2:52 pm
March 30th, 2011 at 7:37 am
Neat writing style, did you take a college course for it?
September 3rd, 2011 at 12:23 am
I’ve been on your site for a while, and really like it. I guess its about time I let you know my appreciation. Keep up the good work. This answered some questions I had. Is there an RSS feed I can subscribe to?
September 12th, 2011 at 9:12 am
I have a problem.. can someone please help.. I have a data source whose output is fed to a script component. The problem is when there are any new columns are added to the source. ScriptComponent does not take the newly added columns as inputs automatically. we should manually check the input columns. Is there a solution to do it automated. I cannot modify the ScriptComponent everytime a new column is added or Is there any other way to do it in SSIS?
December 3rd, 2011 at 5:39 am
I believe this is one of the so much vital information for me. And i’m satisfied studying your article. However want to remark on few common issues, The web site style is wonderful, the articles is really excellent : D. Good job, cheers
December 28th, 2011 at 10:12 am
Hi
I got SSIS package(dtsx). on the control flow i create script task. every time I get in to (edit script) I need to configure the default reference path(project-> properties-> reference path) to be able to compile the script. the problem is that i cant debug the code. this is the only work station its happen in my team.
what do i need to do?
thanks