Wed 4 Apr 2007
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:
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.WrapperPublic Class ScriptMain
Inherits UserComponentPublic Overrides Sub CreateNewOutputRows()
MyOutputBuffer.AddRow()
MyOutputBuffer.MyColumn = 123
End SubEnd Class
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/