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

To do this. click on the little icon in the upper left-hand corner of the Variables window. . create a new SSIS project and call it what you will. open it now by going to View --> Other Windows --> Variables.Inside BIDS (Business Intelligence Development Studio). If your Variable window is not already open. Open Variables Window Now let's create a variable. Name the variable LastSalesOrderID.

you should now see it in the Variables window. can have different scopes. For instance. Make sure the scope of the variable is the name of your project.) . like in other programming languages. (Variables in SSIS. Once you've confirmed that the variable exists. this means the variable is defined at the package scope. create an Execute SQL task. which is "Blog" in my case (for obvious reasons). 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 a variable After you create the variable.

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

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

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

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

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

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

the testStage table will return the following results: Select * From testStage.-------.in/2011/03/execute-sql-task. SalesOrderDetailID ProductId OrderQty LineTotal -----------------.blogspot.Configure OLE DB Destination At this point.html . Upon successful execution.----------. http://sqlserver360.-----------------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. you should be able to successfully execute your package.

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

In Execute SQL Task. So whole result set will be stored in FileInfo object variable .. Resultset should be Full result set . set the result set like this. set the following property.In Execute SQL Task.

In Foreach loop editor.In Foreach loop editor. set the following properties .Data Flow Task should look like this . few more settings in Variable Mappings ..

In Destination.blogspot.Derived column we can set like this. go to properties --> Expression --> ConnectionString and set following value.. @[User::FilePath] + "\\" + @[User::FileName] We can use Expression builder also for this. .html . we can do normal mapping .In Flatfile connection manager. .Run the package and check the result in "ForEachLoopDemoLoad" table http://sqlserver360.in/2011/03/pivot-transformation-in-ssis.

Sign up to vote on this title
UsefulNot useful