How to create a data synchronization task using SSIS

Source: Internet
Author: User
Tags ssis

These packages can be used independently or together with other packages to meet complex business needs. Integration services can extract and convert data from multiple origins (such as XML data files, flat files, and relational data sources) and then load the data to one or more targets. (From msdn, for more details, refer to: http://technet.microsoft.com/zh-cn/library/ms141026 (V = SQL .105). aspx)

Next I will use SSIS to demonstrate a practical example. For example, if I have a database, the data of this database will be regularly migrated to other databases for backup data or other purposes. During migration, some new fields will be inserted into the backup database, and some modified fields will be modified in the backup database. Now we will use SSIS to complete this task.

First, run the following SQL statement in my source database db_source and the target database db_destination to create the required table. This table test_1 is used for demonstration.

CopyCode The Code is as follows: Create Table [DBO]. [test_1] (
[ID] [int] identity (1, 1) not null primary key,
[Name] [varchar] (50) null,
[Age] [int] Null
)

After the table is created, you can add several records to the source data table. The target database is empty.

Now we open Vs and create an intergration Services Project. (Note: If SQL Server is installed with express, this project template is not available)

After creating a project, drag a Data Flow task under the control flow tab, for example:

Double-click the data flow task and we will enter the data flow tab.

Then we find the ole db source in the left-side toolbar and drag two ole db sources. Name them source dB and destination dB respectively.

Drag the data source and double-click it to set it. It is mainly used to link the database and select the table or view you want to migrate. I will not detail it here. Note that, as shown in the following figure, if a red X is displayed on a graph, the setting is incorrect.

The next step is to drag two sort and one merge join, point the previous data source arrows to two sort respectively, and input the data from the last two sort to the merge join at the same time.

Double-click two sort fields, select the table ID, and sort the field ID once. Because the merge join process requires that the input data be sorted. You can set the sortkeyposition attribute for the output fields in the data source to sort the data. (See: http://msdn.microsoft.com/zh-cn/library/ms137653.aspx)

The first time we pull the arrow from sort to merge join, we will choose whether the input data is left input or right input, as shown in the figure below, the left is the left input, and the right is the right input. Then we double-click merge join and set it as shown in:

The data output after this process is checked here. The join type must be left Outer Join, because the left side is our original data table and the right side is our backup table, the right table can be viewed as a subset of the Left table. If the left table has data but the right table does not, the data of the backup database needs to be inserted.

Now we need a branch, that is, the new data needs to be inserted into the backup database, and some data needs to be updated to a new value. We drag a conditional split from the toolbar for such branch processing. We direct the output in merge sort to conditional split, and double-click conditional split, as shown in. (Note that one condition is isnull and the other is non-isnull ).

In this case, their input values are divided into two types of conditional outputs. Finally, we drag an ole db destination to insert data and an ole db command to update the database. The final process is as follows:

Double-click Set ole db destionation and select the tables in the target database for data import. Note that you must select the keep identity option, because I used the auto-increment attribute for the ID field when creating the table.

Double-click Set ole db command. First, select the link object in the Connection Manager tab, and then set your sqlcommand attribute in the component Properties tab. For example:

Are the parameter values used here? And then set the column in the column Mappings tab to replace the actual value, for example:

At this point, the task has been created, and no code has been written. The task is dragged directly. Now you can directly press F5 in vs to see the effect. Our target data table will be inserted into the value in the source data table. Then, let's modify the original data table and run the preceding task to view the changes in the target database.

So how do I regularly complete the task? Here, you can use the SQL Server Agent to call the package we wrote above, and the supervisor uses dtexec.exe to execute the preceding task in the windowsplanning task.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.