Saturday 18 April 2020

Create SSIS package

In this demo, will create an SSIS package that fetches data from the database using stored procedure and write data in text file.

In real time requirement is different like call web API in package etc.. 

Lets Start

Step 1:- Open Visual Studio 2015

Step 2:- Go to File => New => Project...


















Step 3:- Open SSIS Toolbox => Drag or Double click on Execute SQL Task in left pane 


















Step 4:- Double Click on Execute SQL Task in middle pane 
































Step 5:- Set ResultSet => Full result set => Set Connection => Click on <New Connection...> =>Click on New...



































Step 6:- Set SQLStatement 
































Step 7:- Click on Result Set left pane => Click on Add => Set Result Name 0 => Click on Variable Name => Click on OK 


































Step 8:- Open SSIS Toolbox => Drag or Double click on Foreeach Loop Container in middle pane

Note:- 
Here Using Foreeach Loop Container to handle multiple rows return by Query or Procedure.  


















Step 9:- Drag Script Task inside the Foreeach Loop Container in middle pane 


















Step 10:- Connect Execute SQL Task arrow to Foreeach Loop Container 


























Step 11:- Double Click on Foreeach Loop Container => Select Collection in left pane => Click on Enumerator and choose Foreach ADO Enumerator => Set ADO object source variable: 
































Step 12:- Select Variable Mappings in left pane => Variable => Select <New Variable...> => Click on OK 

































Note:- Create one more variable Date with type string and that variable use to map with collection data sequence.

Step 13:- Double Click on Script Task => Set ReadWriteVariables => Click on Edit Script... 























Step 14:- Add following code in Edit Script main function

//Read Date value
string date = Dts.Variables["User::Date"].Value.ToString();
string path = @"c:\Demo\";

//Write to path
if (!System.IO.Directory.Exists(path))
    System.IO.Directory.CreateDirectory(path);

System.IO.StreamWriter sw = new System.IO.StreamWriter(path + "WriteText.txt"true);
sw.WriteLine(date);
sw.Flush();
sw.Close();





















Note:- To debug the code apply debugger. Press Ctrl + S to save file and close windows.

Step 15: Click on OK 
































All Done
Run Project
Output will be write in the C:\\Demo\\




Add Local Variable

































SQL Script

CREATE PROCEDURE proc_GetLastFiveDate
AS
BEGIN
DROP TABLE IF EXISTS #TEMP
CREATE TABLE #TEMP(ID INT IDENTITY, DATE DATETIME)
INSERT INTO #TEMP(DATE)
SELECT GETDATE() 'DATE'
UNION ALL
SELECT DATEADD(DD,-1,GETDATE())
UNION ALL
SELECT DATEADD(DD,-2,GETDATE())
UNION ALL
SELECT DATEADD(DD,-3,GETDATE())
UNION ALL
SELECT DATEADD(DD,-4,GETDATE())

SELECT CAST(DATE AS VARCHAR(50)) FROM #TEMP
END


No comments:

Post a Comment