Thursday, January 31, 2013

SSIS pass a child package varaible value to the parent package

Introduction

my project has two package , one is child package , the other is parent package , that means in the parent package , the child package will be executed , below is data flow screen shot , in the child package , i created a variable called child_row_count , which will assign row count number

 
next is parent package , which will execute the child package , and send the child_row_count variable's value to me by email , note that there need create another variable called parent in parent package
 
next step is create script in child package , which was used to assign child package variable "child_row_count" to parent package variable "parent"
 
 
1 setup the ReadOnlyVariables and ReadWriteVariables
note that ReadWriteVariables should be parent package variable , but they are not in the list , you have to manually type in without "User:: "
 
 
2 script
 
Dts.Variables(
"parent").Value = Dts.Variables("child_row_count").Value

if you want to pass the parent package variable to child , you also can use this approach



 


SSIS: Reading and Writing to Variables in Script Task


 

SSIS: Reading and Writing to Variables in Script Task          

SSIS: Reading and Writing to Variables in Script Task

A lot of people still consistently ask me about how to read and write to variables in the SSIS script task. In this post I will demonstrate for you the two ways in which you can go about this, one from native SSIS functionality and the other from code. In SQL Server 2008 both will generally work out equally as well. In previous versions, you may want to stick with the coding piece as sometimes the a€˜automatica€™ integration with the variable dispenser was a little hooky.
So for our example, I am going to set up a simple SSIS project. First I create 2 variables: MyName & YourName. I have scoped these at the packages level as it has always been my instinct that unless there is a compelling reason to scope it otherwise.



Now that we have our variables set up, we are ready to begin making our project. Since this example is going to be pretty straightforward, we will just drop two script components onto the control flow design surface. The first will be the one using the native way of handling variables in the script component and the second will be using a little bit of custom code to do it programmatically. The setup is simply shown below.


Now opening up the script task you can click on the a€| button next to the readonlyvariables and readwritevariables areas. For this sample, I am setting up the MyName variable to be read only and the YourName variable to read-write.

 



When completed the screen will show our two variables in the boxes as shown below.



Now it is a matter of simply making a call, like below, to access your variables. For my example I am simply going to display the values in some message boxes. Reading from the MyName variable and overwriting the YourName variable. Pretty simple.
    Public Sub Main()
        Dim MyName As String = Dts.Variables("MyName").Value
        MsgBox(MyName)
        Dts.Variables("YourName").Value = "Tom"
        Dim YourName As String = Dts.Variables("YourName").Value
        MsgBox(YourName)
        Dts.TaskResult = ScriptResults.Success
    End Sub




The variables integration with the script tasks has gotten much better in SQL Server 2008 and I have yet to run into any problems like I have previously. Still, there may be some instances that you come across that you would rather programmatically access variables. For this the code below should do the trick. I have two functions: one for reading a variable and the other for writing to a variable. You will notice in both instances that you must lock the variable first before trying to access it. Think of it as SSISa€™s version of row locking. You want to make sure that you are getting the most accurate version of the variable at that point in time. What good would it do you,since things can run in parallel, if another task is updating the variable at the same time you are trying to read it?
    Public Sub Main()
        Dim MyName As String = ReadVariable("MyName")
        MsgBox(MyName)
        WriteVariable("YourName", "Tom")
        Dim YourName As String = ReadVariable("YourName")
        MsgBox(YourName)
        Dts.TaskResult = ScriptResults.Success
    End Sub
 
    Private Function ReadVariable(ByVal varName As String) As Object
        Dim rtnValue As Object
        'Create a variables collection to hold you object
        Dim var As Variables
 
        Try
            'Lock the variable first to make sure that you have exclusive access
            'Think of it like a database object lock
            Dts.VariableDispenser.LockOneForRead(varName, var)
 
            'Now populate your result
            rtnValue = var(varName).Value
        Catch ex As Exception
            Throw ex
        Finally
            'You must make sure that you unlock the variable before exiting routine
            var.Unlock()
        End Try
 
        Return rtnValue
    End Function
 
    Private Sub WriteVariable(ByVal varName As String, ByVal value As Object)
        'Create a variables collection to hold you object
        Dim var As Variables
 
        Try
            'Lock the variable first to make sure that you have exclusive access
            'Think of it like a database object lock
            Dts.VariableDispenser.LockOneForWrite(varName, var)
 
            'Now populate your result
            var(varName).Value = value
        Catch ex As Exception
            Throw ex
        Finally
            'You must make sure that you unlock the variable before exiting routine
            var.Unlock()
        End Try
 
    End Sub

Please note on the code above it is also possible to use something like the following for locking and getting the variable into your collection
  Dts.VariableDispenser.LockForRead(varName)
  Dts.VariableDispenser.GetVariables(var)

But it includes an extra step so it is not the most elegant solution. Additionally, you may notice that in the Finally block. This is not required as Microsoft a€˜promisesa€™ in their documentation that variables are automatically unlocked when the execution of the script task stops. So you can trust that will happen or call it explicitly like me just so that you will sleep better at night.
Hopefully, this helps out some of my colleagues out there that may be struggling finding a good outline of this in the documentation.


请用Ctrl+C复制后贴给好友。

由于IE浏览器升级禁用了alt+x快捷键,请用alt+q快捷键来快速进入写说说入口

SSIS-VB.net connect to Oracle


 

SSIS-VB.net connect to Oracle          
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim dr As OleDbDataReader

myConnection
= New OleDbConnection("Provider=MSDAORA.1;UserID=xxxx;password=xxxx; database=xxxx")
'MSDORA is the provider when working with Oracle
Try
myConnection.Open()
'
opening the connection
myCommand
= New OleDbCommand("Select * from emp", myConnection)
'executing the command and assigning it to connection
dr = myCommand.ExecuteReader()
While dr.Read()
'
reading from the datareader
MessageBox.Show("EmpNo" & dr(0))
MessageBox.Show("EName" & dr(1))
MessageBox.Show("Job" & dr(2))
MessageBox.Show("Mgr" & dr(3))
MessageBox.Show("HireDate" & dr(4))
'displaying data from the table
End While
dr.Close()
myConnection.Close()
Catch ee As Exception
End Try
 

SSIS Save output file as the dynamic name like file .txt



SSIS Save output file as the dynamic name like file <yyyymmdd>.txt          

 

Introduction

One of my project team members came up with a requirement, wherein she was developing a SSIS package to generate text file as output. The source is a Microsoft-SQL Server 2005 database. She was using a T-SQL Query to read data from the data source. The text file should be saved in file system with dynamic name. For example, File<yyyymmdd>.txt.

Solution

The Package consists of DataFlow tasks to generate, Header, Body and Footer for the flat file (since the requirement demands the flat file to have 3 sections: header, body/details, footer with different set of data from the database) and a File System Task to set the name of the file dynamically.
Define a variable with the package scope. To create a variable, right click on the Control Flow workspace and click on Variable. It will open the variables tab and from there, we can create variables.
Go to the properties of the variable and click on the button beside the Expression property.
The Expression Builder dialog box opens up. Here we need to build the expression for the dynamic file name.
In the Expression Text Box, put in either of the following expressions:
"C:\\FlatFile\\File" + SUBSTRING( (DT_WSTR,30)GETDATE() , 1, 4 ) + SUBSTRING( (DT_WSTR,30)GETDATE() , 6, 2 ) + SUBSTRING( (DT_WSTR,30)GETDATE() , 9, 2 ) + ".txt" 
OR
"C:\\FlatFile\\File" + SUBSTRING( (DT_WSTR,30)GETDATE() , 1, 4 ) + SUBSTRING( (DT_WSTR,30)GETDATE() , 6, 2 ) + SUBSTRING( (DT_WSTR,30)GETDATE() , 9, 2 ) + ".txt" 
There is a button in the Expression Builder dialog box called “Evaluate Expression”. Click on the button to check the file name and to validate whether the expression is correct or not. And then, click on OK. Refer to the following screenshots:
 
 
Now finally, use the variable in the File System Task (Rename File). Double click on the File System Task in the Control Flow to open up the File System Task Editor and in the Destination Connection section, set IsDestinationPathVariable to True and select the defined variable corresponding to the DestinationVariable property. Refer to the following screenshot:
 
由于IE浏览器升级禁用了alt+x快捷键,请用alt+q快捷键来快速进入写说说入口