Category Archives: SSIS

SQL Server – Integration Service – How To Extract Images From a SQL Server Table To a Folder Using SSIS

In this article we will go through how we can extract images from a table in SQL Server and copy it on a folder. To achieve this we need to use SQL Server Integration Service. Let’s go over the process step by step.

Step 1: Create a new SSIS project in BIDS
IE1

Step 2: Drag and drop the Data Flow Task from the SSIS Toolbox to the design surface

IE2

Step 3: Go to the Data Flow tab. Drag and Drop the following on the design surface.

ie3

Step 4: Right click on the connection manager area and create a new OLEDB connection. We will create a connection for the AdventureWorks2012 database and the Production.Photo table.

 

IE4 IE5

 

Step 5. Use the following command to select the data from the table

declare @Ipath varchar(100)= 'C:\document\'
SELECT [ThumbNailPhoto],
@Ipath+[ThumbnailPhotoFileName] AS Imagepath
FROM [AdventureWorks2012].[Production].[ProductPhoto]

 

IE6

 

Step 6. Double click on the export column transformation editor and select the following values

IE7

 

Step 7. Build the package and execute. You should find all the images in the selected folder

IE9

 

SQL Server Integration Services – Foreach Loop Container

In my previous article I wrote about the For loop Container. In this article I will show how we can implement the Foreach loop container with a demo. The Foreach loop container is the third container amongst the three containers in SSIS.

The Foreach loop container can be used to loop through a set of enumerated objects like files in a folder or rows in a table. We can add different control flow tasks within the container for each enumerator or a DataFlow task to process data related to each item. Let us now implement this container with a small demo.

Implementing Foreach Loop Container

Objective: To copy files from one folder to another folder.

Step 1: We will be using the package we created in my article. Drag and drop a Foreach loop container in the design UI surface -> Drag and drop a File system task inside the container.
IS48Step 2 : Create a variable as shown below with the depicted data type and value
IS49Step 3: create 2 folders named ‘Source’ and ‘destination’ respectively. In the source folder create two files named file1.txt and file2.txt
IS50 IS51Step 4: Right click -> Choose Edit on the Foreach loop container and fill up the following as depicted below. Click on OK.
IS52 IS53Step 5: Right Click on the File System task -> On DestinationConnection -> Choose the usage type as ‘Existing Folder’ -> Choose the path of the destination folder -> click on ok.
IS54Step 6: Set the rest of the fields as below -> Click on OK.
IS55Now we are ready to test out solution. Press F5 and the 2 files should be in the destination folder.
IS56 IS57Bingo!!!! The files has been copied to the destination folder. I hope this article was useful in understanding the Foreach loop and its understanding.

SQL Server Integration Services – For Loop Container

In my previous article we discussed about the Sequence container. In this article we will implement the For loop container. The objective of this container is that it will iterate through all the tasks inside until the condition is satisfied. For every  iteration the For Loop container evaluates the condition that is specified and repeats the process until the condition stands false.

The For loop container has 3 expressions that determines the number of executions that container would undergo. let us understand these expressions briefly.

InitExpression:

This is an optional expression in the For loop container. It is evaluated before any process is started inside the loop. The major use of this expression is to set an intial value as the name indicates.

EvalExpression:

The second expression when the loop starts. It is also evaluated before any process is executed inside the container, and then again evaluated at the beginning of each loop. This determines if the loop continues or ends.
If the condition stands TRUE, the process is executed again. If the condition stands FALSE, the loop ends.

AssignExpression:

The is the 3rd condition used in the For Loop. It is used to change the value of the variable used in the EvalExpression. This expression is evaluated for each pass through the loop as well, but at the end of the workflow. This condition is optional.

Implementation of the for loop Container

Lets implement the For loop container. I will be using the project I created in the my article.

Step 1: Open the project in BIDS -> Add a Execute SQL task on the UI design surface. Insert the following script in the task

CREATE TABLE Forloop
( id INT IDENTITY(1,1) NOT NULL,
Mydate DATETIME
)

Step 2: Drag and drop a For loop container inside the design UI surface -> Drag and drop a Execute SQL Task inside the For Loop Container -> Insert the following code inside the Execute SQL Task

INSERT INTO Forloop VALUES (GETDATE())

Step 3: Click on the variable tab -> create a variable called counter with data type as Int64 or Int32.
IS44Step 4: Right Click inside the For loop container and click on edit. Fill in the expressions as depicted in the pic below -> Click on OK.
IS45Step 5: Press F5 and make sure the execution succeeds
IS46
What we observe in the database is that the table has been created with 10 rows inserted in the table.
IS47
In this is article I have tried to implement the For Loop container in a very simplified manner. I hope I was able to do that.

 

 

 

 

 

SQL Server Integration Services – Sequence Container

In this article we will understand how we can use a Sequence Container with a demo. In my previous article I wrote about Control Flow Objects in SSIS.

Sequence Container and its benefits

Sequence container groups related tasks in a package and helps in understanding the complexity of a package in a simpler way. The objective of the Sequence container is to have multiple separate control flows grouped together in the package.

Some of the benefits of the package are as below:
1) Helps in debugging.
2) If a package has multiple tasks then it is easier to group the tasks in Sequence Containers and we can collapse and expand this container for usability.
3) We can set the property of entire container rather than setting the property of individual tasks.
4) We can set the flexibility of completion of the process in the container to a transaction level i.e in case of an error the entire process can be rolled back or partial success can be achieved.

Implementing Sequence Container

Now we will implement the sequence container with a demo. In the first example we will not use TransactionOption but in the second example we will. Let us begin. We will be using the project we created in my article.

Step 1: Open the SSIS Project -> Click on the package MyPackage.dtsx -> Drag and drop a Sequence Container from the toolbox on the design surface -> Drag three Execute SQL Task objects in the Sequence ContainerIS32Step 2: Rename the 1st Task as Create Table. Rename the second task as Insert Values. Rename the 3rd Task as Update Values -> Drag the constraint from the 1st task onto the Insert Values Task and then drag the constraint from the Insert Values Task onto the Update Values Task. The structure should look like below
IS33Step 3: Right click on the Create Table Task -> Click Edit -> Change the connection to AdventureWorks2008 -> Click on the SQLStatement and insert the following code -> Click on OK.

CREATE TABLE [SequenceContain]
(
id INT NOT NULL,
NAME VARCHAR(20),
age INT
)

Step 4: Implement the same for Insert Values Task apart from the SQL. Insert the below SQL code

INSERT INTO dbo.SequenceContain VALUES
(1,'Amit A',34),(2,'Amit B',34),
(3,'Amit C',24),(4,'Amit D',44),
(5,'Amit E',64),(6,'Amit F',34)

IS34Step 5: Implement the same for Update Values Task apart from the SQL. Insert the below SQL code

UPDATE SequenceContain
SET name = 'Bodhi'
WHERE id = 3

IS35Step 5: Press F5  and the package execution begins. The entire package will turn green when the execution succeeds
IS36Step 6: Lets verify via SQL Server if the table and data has been created and modified.
IS37

TransactionOption

Example 1:

Now lets tweak the third task i.e Update Values task and enforce an error. We will change the TransactionOption to NotSupported.

Step 1: Right click on the Update Task -> Goto SQLStatement -> insert the following code below

UPDATE SequenceContai
SET name = 'Bodhi'
WHERE id = 3

We have kept the name of the table incorrect to enforce the error.

Step 2: Select the Sequence Container -> On the properties tab -> TransactionOption -> Select Not Supported.
IS38Step 3: Not lets drop the table from the database so that the create table does not error out.
IS39Step 4: Press F5 and run the package and lets observe what happens
IS40

Observation: What we observe is that the package has failed(red) and the update values Task has also failed(red). Let now have a look at the database and see what impact has this package made.
IS41What we observe is that create table and insert values task has succeeded but the update values task has failed. So this what NotSupported does. It does not do a full roll back.

Example 2:

Now we will change the TransactionOption to Required and observe what happens
Step 1 : Drop the table from the database
Step 2 : Change the TransactionOption to Required
IS42Step 3: Press F5.
IS40
Observation:

The Package fails but lets verify what has happened at the database level by running a select on the table.
IS43
The entire process has been rolled back. The table structure does not exist even though the create table had succeeded. So this is what The Required option does. In case of an error it rolls back the entire package.

In this article I have tried to explain the use of the sequence container in a simplified manner. I hope i was able to do that.

 

 

SQL Server Integration Services – Control Flow Objects

In my previous article on SSIS we spoke about Packages,Connections and Data Sources. In this article we will talk about Control Flow Objects and how we can use them.

So what is Control Flow?

Control Flow is the engine which manages the workflow of the tasks created coupled with the control flow containers and constraints. SSIS provides a Design UI which displays a workspace where we can configure different control flow objects.

SSIS provides three types of control flow objects
1) Tasks – Tasks are objects that perform specific work
IS16
2) Containers – Containers help group different tasks
IS17
3) Constraints – Constraints help connecting different tasks and containers. Its also helps to define the order of execution of different tasks.
IS18

A package must contain at least one task that performs a certain operation. When multiple tasks are configured, then a container can be used to group them together.
IS19

 

Container

SSIS provides three types of Containers. Here is a brief description about the three Containers. We will deep dive into the Containers later.

1) Sequence Container : This allows us to group multiple tasks together. The benefits of this feature is that we can control the entire group rather that managing each task separately. It also means we can run different tasks sequentially or parallel. The entire group can collapsed or expanded. The Sequence container is the 3rd container in the toolbox
IS202) For Loop Container : This provides the same functionality as the sequence container but also lets you run tasks multiple times based on the condition provided. The For Loop is the 1st tool in the tool box
IS213) Foreach Loop Container : This allows looping but instead of a condition the looping happens over objects such as files and folders or tables in a database.
IS22

Implementation of Control Flow Task

We will now implement the above by creating and editing a control flow task and executing the package within the SSIS environment. We will be using the same project we created in our previous article for this demo.

Step 1: Open the Project using BIDS -> Select Mypackage.dtsx -> Click on the Toolbox -> Drag and Drop the Execute SQL Task on the Designer Surface. Rename the task as Update Table
IS23 IS24
Step 2: Right Click on the Task – > Click on Edit -> Change the Connection to AdventureWorks -> Click on the SQLStatement and insert the below query – > Click on OK.
IS25 IS26 IS27Query:

UPDATE Production.ScrapReason SET
ModifiedDate = '2014-06-01 00:00:00.000'
WHERE name = 'Drill pattern incorrect'

Step 3: click Start Debugging on the Standard toolbar -> If the execution is correct then the task would be green.
IS28 IS29Step 4: Click on the Progress tab to view the execution details of the task. -> Stop the execution by clciking on the stop debugging button – > Save the project.
IS30 IS31

The objective of this article was an introduction to the concept of Control Flow and Tasks with a small demo. I hope I was able to do that. In my next article I will write about Container with a broader perspective.

 

 

SQL Server Integration Services – Packages, Datasources and Connections

In this article we will see how we can create a SSIS project via visual studio and then create a package and linking the packages to data sources along with connections. For this demo i will be using SQL Server 2008. The steps remain same for SQL Server 2008 R2 and SQL Server 2012.

creating a SSIS project…

Step 1: Open SQL Server Business Intelligence Development Studio(BIDS). Go to File -> New -> Project
IS1

 

 

 

 

 

 

 

Step 2 : ON the new Project dialog box choose Integration Services Project. Give the Project a name and location as shown below. Click on OK. The  Project will appear on the Solution explorer. By default a package is already created named Package.dtsx
IS2IS3

Step 3: let us rename this package as MyPackage1. Right click on the Package and select rename. Change the name to MyPackage.dtsx. Click Yes on the confirmation dialog box.
IS4IS5
Step 4: We will create two more packages named DC.dtsx and DP.dtsx. Right click on SSIS package on solution explorer and click on New SSIS Package. A package would be automatically added with default name Package(n).dtsx. We will rename the package as DC.dtsx. Repeat the same steps and create another package named DP.dtsx. Click on save all button to save all the changes made.
IS6 IS7So this is how we will create a SSIS project with packages. In the next section we will create data sources for the project.

 creating data sources..

Step 1: Open the Project in BIDS. Right click on Data Sources and select New Data Sources. On the Data source wizard click on new and select a server name and database name. In this case we will choose AdventureWorks as our database.
IS8 IS9 IS10Step 2: Click on OK. Click Next and click on Finish. The new data source will now appear under Data Sources
IS11 IS12 In the next section we will create new package connections from the Data Sources we created.

Creating Connections

Step1 : Click any one of the packages. Right Click on the connection managers window and select New Connection from Data Source. Select the Data source we created name AdventureWorks. Click on OK.
IS13 IS14The new connection connection will now appear in the connection managers window for the package. We need to create the same for the rest of the two packages.
IS15

 

%d bloggers like this: