How to Insert or Update records in SSIS DataFlow
2008-09-18 14:46
423 查看
If you import Data from a specified Source periodically in an Integration Service Project and you need to update existing data in the SQL Destination the best workaround for this is as following:
First define a Source in the data flow e.g. Ole DB Source, make sure that your Columns does not contain NULL Values, this you can do with the Derived Column Shape then add a Script Component to the Data Flow - now your Data Flow should look like the following picture:
In the Script Component you have to define the input source and the output source. Right click on the Script Component and click 'Edit', then go to 'Inputs and Outputs' you can rename the Input and Output Name. In the Output you should set the Exclusion Group to 1 and the SynchronousInputID to the ID of the Input in this case the ID is '243' per default the first output has the ID. In the following Picture you see the configuration.
The next step:
Go to the option Script and click the button Design Script, now Visual Studio will be open where you can define your source code, the default language is VB:
Here is a little code snippet for a Insert Update procedure:
Dim reader As SqlClient.SqlDataReader
sqlConn = New SqlClient.SqlConnection("Data Source=Servername;Initial Catalog=Databse;Integrated Security=True")
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand("SELECT * FROM Tabelle WHERE(ID = @ID)", sqlConn)
sqlParam = New SqlClient.SqlParameter("@ID", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@ID").Value = Row.Bestellnummer
reader = sqlCmd.ExecuteReader()
If reader.Read() Then
'do all field comparisons here to determine if
' the record changed since the last ETL.
If (reader("AmountOfHardware").ToString() <> Row.Summe) Then
reader.Close()
sqlCmd = New SqlClient.SqlCommand("Update TBestellmassnahme Set AmountOfHardware = @Sum Where ID = @ID ", sqlConn)
sqlParam = New SqlClient.SqlParameter("@Sum", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@Sum").Value = Row.Summe
sqlParam = New SqlClient.SqlParameter("@ID", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@ID").Value = Row.Bestellnummer
sqlCmd.ExecuteNonQuery()
Else
End If
Else
'if the reader contains no data the row will be redirect to 'the output source which could be the Insert Statement
Row.DirectRowToOutputInsert()
End If
reader.Close()
sqlConn.Close()
After you have insert the script you should add an OLE DB Command Shape to the Output of the Script Component. In this Command Shape you could define the Insert Statement as you need.
Ref :http://developers.de/blogs/nadine_storandt/archive/2006/12/07/How-to-Insert-or-Update-records-in-SSIS-DataFlow.aspx
First define a Source in the data flow e.g. Ole DB Source, make sure that your Columns does not contain NULL Values, this you can do with the Derived Column Shape then add a Script Component to the Data Flow - now your Data Flow should look like the following picture:
In the Script Component you have to define the input source and the output source. Right click on the Script Component and click 'Edit', then go to 'Inputs and Outputs' you can rename the Input and Output Name. In the Output you should set the Exclusion Group to 1 and the SynchronousInputID to the ID of the Input in this case the ID is '243' per default the first output has the ID. In the following Picture you see the configuration.
The next step:
Go to the option Script and click the button Design Script, now Visual Studio will be open where you can define your source code, the default language is VB:
Here is a little code snippet for a Insert Update procedure:
Dim reader As SqlClient.SqlDataReader
sqlConn = New SqlClient.SqlConnection("Data Source=Servername;Initial Catalog=Databse;Integrated Security=True")
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand("SELECT * FROM Tabelle WHERE(ID = @ID)", sqlConn)
sqlParam = New SqlClient.SqlParameter("@ID", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@ID").Value = Row.Bestellnummer
reader = sqlCmd.ExecuteReader()
If reader.Read() Then
'do all field comparisons here to determine if
' the record changed since the last ETL.
If (reader("AmountOfHardware").ToString() <> Row.Summe) Then
reader.Close()
sqlCmd = New SqlClient.SqlCommand("Update TBestellmassnahme Set AmountOfHardware = @Sum Where ID = @ID ", sqlConn)
sqlParam = New SqlClient.SqlParameter("@Sum", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@Sum").Value = Row.Summe
sqlParam = New SqlClient.SqlParameter("@ID", SqlDbType.NVarChar, 50)
sqlCmd.Parameters.Add(sqlParam)
sqlCmd.Parameters("@ID").Value = Row.Bestellnummer
sqlCmd.ExecuteNonQuery()
Else
End If
Else
'if the reader contains no data the row will be redirect to 'the output source which could be the Insert Statement
Row.DirectRowToOutputInsert()
End If
reader.Close()
sqlConn.Close()
After you have insert the script you should add an OLE DB Command Shape to the Output of the Script Component. In this Command Shape you could define the Insert Statement as you need.
Ref :http://developers.de/blogs/nadine_storandt/archive/2006/12/07/How-to-Insert-or-Update-records-in-SSIS-DataFlow.aspx
相关文章推荐
- How to Update millions or records in a table", version 8.1.7
- How to use insert or retrieve data by using Core Data in iOS
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.
- How to Take Ownership of a File/Folder Using Command-Line in Windows 7 or newer version System
- IDENTITY_INSERT的打开和关闭(how to set IDENTITY_INSERT to OFF or ON)
- How to read Mac .DMG file in Linux or PC
- How to replace # or Not assigned with blank in WAD
- How to create an Excel sheet or chart in VC?
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- How to install pam_mysql in CentOS or Red Hat
- 在centos7上面组安装出现No packages in any requested group available to install or update错误解决方法
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- How to verify ntp service properly or not in linux/unix
- How To Use FETCH_RECORDS In Oracle Forms
- How To Enable or Disable Simple File Sharing in Windows XP
- Spring MVC – How to include JS or CSS files in a JSP page
- How to update AX 2012 Enterprise Portal in a multi-server web farm
- How to Delete or Add Resource in 11gR2 Grid Infrastructure [ID 1069369.1]
- How to distinguish between strings in heap or literals?