user1709091
user1709091

Reputation:

How to automate the execution of a stored procedure with an SSIS package?

I have a stored procedure that gets executed through SQL SSIS using a Execute SQL Task.

The task has the following:

USE [OPPY_DWUSD]
GO
DECLARE @return_value int
EXEC    @return_value = [dbo].[generate_merge_scdbk]
    @Schema = N'dim',
    @Dimension = N'VARIETY',
    @ETLSchema = N'stg',
    @ETLTable = N'vw_VARIETY',
    @Execute = 1

SELECT  'Return Value' = @return_value
GO

Right now the way I have this setup, I have multiple Execute SQL Tasks with the same code but different values, about 20 Execute SQL Tasks.

Is there a more cleaner way to pull this off?

Upvotes: 2

Views: 17407

Answers (2)

user756519
user756519

Reputation:

Here is one way of doing this. The example uses SSIS 2008 R2 with SQL Server 2012 backend.

Create a table to store your parameter values. Let's say the table name is dbo.SProcValues. Based on your stored procedure definition, the table schema would look like this.

CREATE TABLE dbo.SProcValues(
    Id int IDENTITY(1,1) NOT NULL,
    SProcName nvarchar(40) NOT NULL,
    SchemaName nvarchar(20) NOT NULL,
    Dimension nvarchar(40) NOT NULL,
    ETLSchema nvarchar(20) NOT NULL,
    ETLTable nvarchar(40) NOT NULL,
    IsExecute bit NOT NULL
) 
GO

Let's insert some sample data using the following script.

INSERT INTO dbo.SProcValues 
    (SProcName, SchemaName, Dimension, ETLSchema, ETLTable, IsExecute) VALUES
    ('dbo.sp_generate_merge', 'dim1', 'dimension1', 'stg1', 'table1', 1),
    ('dbo.sp_generate_merge_scdbk', 'dim2', 'dimension2', 'stg2', 'table2', 1),
    ('dbo.sp_generate_merge_scdbk', 'dim3', 'dimension3', 'stg3', 'table3', 0),
    ('dbo.sp_generate_merge', 'dim4', 'dimension4', 'stg4', 'table4', 0);
GO

On the SSIS package, assuming that you have the data source and connection manager already established. Create the following variables. Variable SProcValues will hold the parameter set that we stored in the above-mentioned table. Variable SQLInnerQuery will hold the query that will be used later in the inner Execute SQL Task. Other variables relate to each column available in the table so we can loop through each row and hold it in a variable.

Paste the following query in the value of the variable SQLGetParameters

SELECT SProcName, SchemaName, Dimension, ETLSchema, ETLTable, IsExecute FROM dbo.SProcValues

Variables

Select the variable SQLInnerQuery and press F4 to view the properties. Set the property EvaluateAsExpression to True and then click the Ellipsis button against the Expression property.

SQLInnerQuery

We need to set an expression that will evaluate to the EXEC stored procedure statement that can be later supplied to the inner Execute SQL Task. Set the following expression.

"EXEC " + @[User::SProcName] + " @Schema = ?, @Dimension = ?, @ETLSchema = ?, @ETLTable = ?, @IsExecute = ?"

If you click Evaluate Expression button on the editor, you can see what the expression will evaluate to. You will also notice that there is no stored procedure name in the below screenshot that is because the package variable SProcName currently does not have any value. During runtime, the SProcName will be assigned with the value from the table and this expression will automatically resolve itself. Expression

On the SSIS package, drag and drop an Execute SQL Task. This task will run the following query to fetch the list of parameter values that are stored in the table dbo.SProcValues. Configure the General page on the Execute SQL Task as shown below. The example uses OLEDB connection and the connection manager/data source is named as Practice.

First Execute SQL Task - General

Configure the Result Set page of Execute SQL Task to store the result set from the query to an object variable.

First Execute SQL Task - Result Set

Now that the first Execute SQL Task is configured to get the list of parameter values that should be passed to the stored procedure, you need to loop through the records.

Drag and drop a Foreach Loop container. Connect the Execute SQL Task's precedence container to the Foreach Loop container. Configure the Collection page of the Foreach Loop container as shown below. We are looping through the result set using the ADO enumerator.

Foreach loop - Collection

Configure the Variable Mappings page on Foreach Loop container as shown below. As we loop through each row, we are storing the column values in respective variables so we can pass it to the next Execute SQL Task to run the stored procedure.

Foreach loop - Variable Mappings

Drag and drop an Execute SQL Task inside the Foreach Loop container so that this task is executed each time we loop through a row in the result set. Configure the Execute SQL Task as shown below.

NOTE

You might want to configure the ResultSet property on this second Execute SQL Task according to your requirements. If you choose ResultSet, then you need to configure an appropriate object variable to accept the result set. I left it as None for this example.

Second Execute SQL Task - General

Configure the values to be passed as parameters to the stored procedure.

Second Execute SQL Task -  Parameter Mapping

Finally, the control flow would look something like this.

Control Flow

When the package runs, the loop will execute the stored procedure for as many records are returned by the SELECT query mentioned above, provided that you have all the stored procedures defined in the table rows are available in the database. I had created the stored procedures dbo.sp_generate_merge_scdbk and dbo.sp_generate_merge with the same parameters definition. That's the reason the package executed successfully.

Execution

Upvotes: 6

billinkc
billinkc

Reputation: 61269

You have the right concept, just need to use some concepts like variables, a foreach loop and parameters on the Execute SQL Task.

Control Flow

Your Control Flow would look something like this

control flow

Variables

I have 6 variables defined in SSIS

  • Dimension | String | VARIETY
  • ETLSchema | String | stg
  • ETLTable | String | vw_VARIETY
  • Execute | Int32 | 1
  • RecordSet | Object | System.Object
  • Schema | String | dim

The first Execute SQL Task will be a query or something enumerable like it. Currently, have a hard coded query to produce the supplied query values. Your solution could just be a chain of SELECT's UNIONed together. The goal of this step is to populate the RecordSet variable.

My Execute SQL Task returns a full result set Execute SQL

and I push that into my object thusly

Result set tab

ForEach Loop Container (ADO Recordset)

The ForEach Loop Container is going to consume that enumerable thing we established beforehand. It will go through each row and we will pop the values out of the object and assign them into local variables.

Change the Enumerator to Foreach ADO Enumerator. Select the object we populated with results User::RecordSet and then use an enumeration mode of Rows in first table

FELC

In the Variable Mappings tab, we will identify the ordinal based location for the values (column 0 maps to variable X). The only trick here is to ensure your SSIS Variable data types match the data type in the result set from your source query. Do note it's a zero based ordinal system.

FELC Variable Mappings

At this point, if you click run you see it enumerate through all the rows you have sent into the RecordSet variable. I find it helpful to run it at this point to make sure I have all of my data types aligned.

Inner Execute SQL Task

I have taken your query and replaced the hard coded values with place holder. An OLEDB connection will use ? while an ADO.NET will use named @varname.

Execute SQL General

In the Parameter Mapping tab, simply map those local variables to the place holders.

Execute SQL Parameter Mapping

Now you have a nice template for running the same proc with varying values.

Upvotes: 3

Related Questions