You are on page 1of 14

Variables In SSIS. Create an SSIS package that used variables to pass data between procs.

Use AdventureWorks; Go Create Procedure dbo.LastOrderGet_sp As Set NoCount On; Begin Select Max(SalesOrderID) As 'LastSalesOrderID' From AdventureWorks.Sales.SalesOrderHeader With (NoLock) Set NoCount Off; Return 0; End Go Create Procedure dbo.ProcessLastOrder_sp @LastOrderID int As Set NoCount On; Begin Select SalesOrderDetailID , ProductID , OrderQty , LineTotal From AdventureWorks.Sales.SalesOrderDetail With (NoLock) Where SalesOrderID = @LastOrderID; Set NoCount Off; Return 0; End Go Create Table dbo.testStage ( SalesOrderDetailID int , ProductId int , OrderQty smallint , LineTotal numeric ); Go

Inside BIDS (Business Intelligence Development Studio), create a new SSIS project and call it what you will. If your Variable window is not already open, open it now by going to View --> Other Windows --> Variables.

Open Variables Window Now let's create a variable. To do this, click on the little icon in the upper left-hand corner of the Variables window. Name the variable LastSalesOrderID.

Create a variable After you create the variable, you should now see it in the Variables window. Make sure the scope of the variable is the name of your project, which is "Blog" in my case (for obvious reasons); this means the variable is defined at the package scope. Once you've confirmed that the variable exists, create an Execute SQL task.

(Variables in SSIS, like in other programming languages, can have different scopes. For instance, a package scope means the variable can be accessed anywhere within the package, but a variable with a Data Flow scope can only be accessed within the specified Data Flow task.)

Create Execute SQL Task Double-click on your Execute SQL Task and configure with the following values: Set "Result Set" to Single Row. Set your Connection to your appropriate data source. Set your SQL Statement to: Execute AdventureWorks.dbo.LastOrderGet_sp;

Set up your Execute SQL Task Now click on "Result Set" and click on "Add." You'll want to put the name of the column that's returned by the proc in the "Result Name" column; in our case, that'll be LastSalesOrderID. Click on the Variable Name column and scroll down until you find the appropriate one (User::LastSalesOrderID).

Mapping the results to a variable Go ahead and add a Data Flow task to the designer surface. We don't need to use a Data Flow task here -- for example, we could use another Execute SQL task instead -- but this will help demonstrate one way to use variables.

Add Data Flow Task Double-click on the Data Flow task and add an OLE DB Source, then double-click on it to open up the properties. Enter the following text in the "SQL Command text" window: Execute AdventureWorks.dbo.ProcessLastOrder_sp ? The question mark (?) tells SSIS to expect a parameter.

Edit OLE DB Source Now click on the Parameters button on the left. This is where we map our variable to our parameter. For the "Parameters" value, enter @LastOrderID (the parameter the stored procedure is expecting). In the "Variables" column, click on the drop-down and navigate to the User::LastSalesOrderID variable.

Map Variables to Parameters Finally, set up an OLE DB Destination, and configure the OLE DB Source to load into the testStage table.

Configure OLE DB Destination At this point, you should be able to successfully execute your package. Upon successful execution, the testStage table will return the following results: Select * From testStage; SalesOrderDetailID ProductId OrderQty LineTotal ------------------ ----------- -------- -----------------121315 878 1 21 121316 879 1 159 121317 712 1 8 Hopefully this gives you an idea of how easy and useful it is to work with variables in SSIS.

http://sqlserver360.blogspot.in/2011/03/execute-sql-task.html

Foreach Loop Container in SSIS


- ForEach Loop Conainter works same as For Loop Container, but only difference is - it will loop the flow for each item within a collection - Here collection can be set of files, ADO.Net resultset or anything EXAMPLE Pre-requisite - Have 3-4 .txt files with simple ID,Name column and 2-3 rows in each file. - You can put those files in different folder also - In database, execute the following code CREATE TABLE ForEachLoopDemoFiles (ID INT, FilePath VARCHAR(255),FileName V ARCHAR(50)) Go INSERT INTO ForEachLoopDemoFiles (ID, FilePath, FileName) VALUES (1,'C:\ForEachLoopContainerDemo\Folder1','a.txt'), (2,'C:\ForEachLoopContainerDemo\Folder2','b.txt'), (3,'C:\ForEachLoopContainerDemo\Folder3','c.txt') Go create TABLE ForEachLoopDemoLoad (ID int, Name varchar(10),FileInfo nvarcha r(255)) How to deal with it? - Create 3 variables.

- In control flow task, create 1 Execute SQL Task which goes further to For Each Loop Container which has internally Data Flow Task

- In Execute SQL Task, set the following property. Resultset should be Full result set

- In Execute SQL Task, set the result set like this. So whole result set will be stored in FileInfo object variable

- In Foreach loop editor, set the following properties

- In Foreach loop editor, few more settings in Variable Mappings

- Data Flow Task should look like this

- In Flatfile connection manager, go to properties --> Expression --> ConnectionString and set following value. @[User::FilePath] + "\\" + @[User::FileName] We can use Expression builder also for this. - Derived column we can set like this.

- In Destination, we can do normal mapping - Run the package and check the result in "ForEachLoopDemoLoad" table

http://sqlserver360.blogspot.in/2011/03/pivot-transformation-in-ssis.html

You might also like