Monday, March 7, 2011

Logging in SSIS


By default whenever package fails, error will be logged in Event viewer.
But still, we can maintain the complete execution history in customized way.





In Text File Log provider, you have specify text file. And it will start logging in that text file. It will append the text at every time.

In SQL Server Log provider, it will create a table called “ssislog”. And it will start inserting records.

Mainly, we are intersted in particular error. So we can do following settings.



Ssislog table will have records like this.



Rowcount Transformation

- It is used to get the total number of records in last transformation or source.
- It puts the value into a variable which we can use further


EXAMPLE

Pre-requisite

Following script has to be executed

CREATE TABLE RowCountDemo (MID INT, MName VARCHAR(10))
CREATE TABLE RowCountDemoOutput (MID INT, MName VARCHAR(10), TotalRecords INT)

INSERT INTO RowCountDemo (MID,MName) VALUES (1,'Nisarg'),(5,'Megha'),(2,'Swara')


Steps

0. Create one variable saying TotalRecords of INT datatype
1. Add Data Flow Task in Control Flow
2. Drag source in DFT and it should point to RowCountDemo table
3. Drag Rowcount transformation and connect it with Source.

4. Make sure to choose created variable which will hold the value
5. Drag Derived Column Transformation and connect it with RowCount.

6. Drag destination and connect it with Derived Column. Have proper column mapping and execute the package and check the result int output table.

Union All Transformation

- It allows us to union (merge horizontally) from more than 1 input
- Unlike Merge Transformation (more details), it allows more than 2 inputs also and inputs are not required to be in a sorted order


EXAMPLE

Pre-requisite

Following script has to be executed

CREATE TABLE UnionAllDemo1 (MID INT, MName VARCHAR(10))
CREATE TABLE UnionAllDemo2 (MID INT, MName VARCHAR(10))
CREATE TABLE UnionAllDemo3 (MID INT, MName VARCHAR(10))
CREATE TABLE UnionAllDemoOutput (MID INT, MName VARCHAR(10))

INSERT INTO UnionAllDemo1 (MID,MName) VALUES (1,'Nisarg'),(5,'Megha'),(2,'Swara')
INSERT INTO UnionAllDemo2 (MID,MName) VALUES (4,'Mulik'),(3,'Dipa'),(8,'Nidhi')
INSERT INTO UnionAllDemo3 (MID,MName) VALUES (7,'Kamlesh'),(6,'Pratibha'),(9,'Mahesh')


Steps

1. Add Data Flow Task in Control Flow
2. Drag 3 source in DFT and they should point to UnionAllDemo1, UnionAllDemo2, UnionAllDemo3 respectively.
3. Drag UnionAll transformation and connect then with all 3 sources one by one. This will not prompt for any input window.
4. Connect UnionAll trans. with Destination which should point to UnionAllDemoOutput table.
5. Whole structure will look like this.

Merge Join Transformation

- It allows us to do normal join (INNER/LEFT/FULL) between 2 SORTED inputs
- input should be sorted first then only it will allow us to do joining
- we can do INNER or LEFT or FULL join between 2 inputs
- RIGHT join can be achieved by swapping the positions of 2 inputs in LEFT Join


EXAMPLE

Pre-requisite

Following script has to be executed

CREATE TABLE MergeJoinDemo1 (MID INT, MName VARCHAR(10))
CREATE TABLE MergeJoinDemo2 (MID INT, City VARCHAR(10))
CREATE TABLE MergeJoinDemoOutput (MID INT, MName VARCHAR(10), City VARCHAR(10))

INSERT INTO MergeJoinDemo1 (MID,MName) VALUES (1,'Nisarg'),(2,'Megha'),(3,'Swara')
INSERT INTO MergeJoinDemo2 (MID,City) VALUES (1,'Ahd'),(2,'Hyd'),(3,'Bom')



Steps

1. Add Data Flow Task in Control Flow
2. Drag 2 sources in DFT and they should point to MergeJoinDemo1 and MergeJoinDemo2 respectively
3. Drag Sort Transformation and connect it with Source1 and specify MID as sorted column
4. Drag Sort Transformation and connect it with Source2 and specify MID as sorted column
5. Drag Merge Join transformation and connect it with Sort1 and select Merge Join Left Input
6. Connect Merge Join transformation with Sort2.
7. Open  Merge Join transformation and select Inner Join, specify Join Key and select columns
8. We have selected only 3 columns so only 3 columns available in output
9. We can choose join from dropdown
10. "Swap Inputs" button will be enabled only in case of Left Join.
TableA LEFT JOIN TableB ----- same as ------- TableB RIGHT JOIN TableA
11. Whole structure will look like this.

Merge Transformation & Sort Transformation

Sort  Transformation
- It takes an input and sorts in ascending / descending order based on specified columns
- more than 1 columns with different sorting order can be specified

Merge Transformation
- It takes exactly 2 SORTED inputs and produces a combined(merged) sorted output
- in simple word / t-sql language, it is nothing but "sorted union all"
- Inputs has to be sorted first then only we can do Merge

EXAMPLE


Pre-requisite

Following script has to be executed

CREATE TABLE MergeDemo1 (MID INT, MName VARCHAR(10))
CREATE TABLE MergeDemo2 (MID INT, MName VARCHAR(10))
CREATE TABLE MergeDemoOutput (MID INT, MName VARCHAR(10))

INSERT INTO MergeDemo1 (MID,MName) VALUES (1,'Nisarg'),(4,'Megha'),(5,'Swara')
INSERT INTO MergeDemo2 (MID,MName) VALUES (2,'Kamlesh'),(3,'Pratibha'),(6,'Nidhi')



Steps

1. Add Data Flow Task in Control Flow
2. Drag 2 sources in DFT and it should point to MergeDemo1 and MergeDemo2 respectively.
3. Drag Sort transformation and connect it with Source1.
4. In Sort transformation, select MID column and specify sorting order as shown below.

5. whatever the columns we check in available input columns, same will be listed below. We can specify different output alias and sorting type.
6. Add another Sort transformation and connect it with second Source
7. Drag Merge Transformation and connect it with 1st Sort transformation and select Merge Input 1.

8. Connect Merge transformation with 2nd Sort transformation.
9. Drag Destination and connect it with Merge Transformation. Destination should point to MergeDemoOutput table.
10. Whole structure will look like this.
11. Execution of package will feed the data in output table like this.

Aggregate Transformation & Multicast Transformation


Aggregate Transformation

- It is same as GROUP BY in SQL
- Using this Transformation, we can get COUNT(*), COUNT DISTINCT, MAX, MIN, AVE, SUM
- Column can be part of GROUP BY / AGGREGATION Function

Multicast Transformation

- It distributes input into more than one output
- Unlike Conditional Split, it is not splitting the output, instead it is sending the all the records to all output branches

EXAMPLE

Pre-requisite

Following script has to be executed

CREATE TABLE AggregateDemo (ADID INT IDENTITY(1,1), StudentID INT, Subject varchar(10), Mark int)
GO
insert into AggregateDemo (StudentID,Subject,Mark)
values (1,'Maths',100),
(1,'Science',100),
(1,'English',90),
(2,'Maths',99),
(2,'Science',99),
(2,'English',95),
(3,'Maths',95),
(3,'Science',100),
(3,'English',98)
GO
CREATE TABLE AggregateDemo_TotalMarkPerStudent (StudentID INT, TotalMark int)
GO
CREATE TABLE AggregateDemo_MaxMarkPerSubject (Subject varchar(10), MaxMark int)
GO


Steps


1. Add Data Flow Task in Control Flow
2. Drag source in DFT and it should point to AggregateDemo
3. Add Multicast Transformation and connect it with source
4. Add 2 Aggregate Transformations and connect them with Multicast Transformation
5. In 1st Aggregate, do following settings.


6. In 2nd Aggregate, do following settings.


7. Add 2 destinations and connect them with 2 aggregate transformations. These 2 dimensions should point to  AggregateDemo_TotalMarkPerStudent & AggregateDemo_MaximumMarkPerSubject respectively.

8. Overall package should look like this.

9. Execute the package. It will look like this.
10. Here you can see that Multicast is transforming all 9 rows in 2 output branches


Audit Transformation


- The Audit transformation lets us add columns that contain information about the package execution to the data flow.
- These audit columns can be stored in the data destination and used to determine when the package was run, where it was run, and so forth
- The following information can be placed in audit columns:
--> Execution instance GUID (a globally unique identifier for a given execution of the package)
--> Execution start time
--> Machine name
--> Package ID (a GUID for the package)
--> Package name
--> Task ID (a GUID for the data flow task)
--> Task name (the name of the data flow task)
--> User name
--> Version ID

EXAMPLE

Pre-requisite
Following script has to be executed in DB

CREATE TABLE AggregateDemo (ADID INT IDENTITY(1,1), StudentID INT, Subject varchar(10), Mark int)
GO
insert into AggregateDemo (StudentID,Subject,Mark)
values (1,'Maths',100),
(1,'Science',100),
(1,'English',90),
(2,'Maths',99),
(2,'Science',99),
(2,'English',95),
(3,'Maths',95),
(3,'Science',100),
(3,'English',98)
GO
CREATE TABLE AuditDemo (ADID INT, StudentID INT, Subject varchar(10), Mark int, ExecutionStartTime DATETIME, UserName NVARCHAR(50), MachineName NVARCHAR(50))
GO


Steps

1. Add Data Flow Task in Control Flow
2. Drag source in DFT and it should point to AggregateDemo
3. Add Audit Transformation and connect it with Source
4. Add Destination and map columns
5. After execution of the package, records will look like this.