kosh
kosh

Reputation: 561

SSIS execute sql task parameter mapping

I'm trying to execute a sql script using the task in SSIS.

My script just inserts a bunch of nambe value pairs in a table. For example -

insert into mytable (name, value) values (?, 'value1'), (?, 'value2')

Now, I want to map a variable defined in SSIS to be mapped to the parameters in the statement above. I tried defining a scalar variable but I guess the sql task doesn't like that. Oh and all the name parameters in the insert statement resolve to a single variable.

For example I want insert into mytable (name, value) values ('name1', 'value1'), ('name1', 'value2')

When I open the Parameter Mapping tab for the task, it wants me to map each parameter invidually like -

Variable Name - User::Name Direction - Input Data Type - LONG Parameter Name - 0 Parameter Size - -1

Variable Name - User::Name Direction - Input Data Type - LONG Parameter Name - 1 Parameter Size - -1

This quickly gets out of hand and cumbersome if have 5-10 values for a name and forces me to add multiple assignments for the same name.

Is there an easy(-ier) way to do this?

Upvotes: 0

Views: 5744

Answers (1)

Francisco Isla
Francisco Isla

Reputation: 56

The easiest (and most extensible) way, is to use a Data Flow Task instead of using an Execute SQL Task.

  1. Add a Dataflow Task; I assume that you have all the variables filled with the right parameters, and that you know how to pass the values onto them.
  2. Create a dummy row with the columns you will need to insert, so use whatever pleases you the most as a source (in this example, i've used an oledb connection). One good tip is to define the datatype(s) of each column in the source as you will need them in your destination table. This will align the metadata of the dataflow with the one the insert table (Screenshot #1).
  3. Then add a multicast component to the dataflow.
  4. For the first parameter/value, add a derived column component, name it cleanly and proceed to substitute the content of your parameters with your variables.
  5. For each further parameter/value that needs to be added; copy the previously created derived column component, add one extra branch from the multicast component and proceed to substitute the column parameter/value as necessary.
  6. Add a union all and join all the flows
  7. Insert into the table
  8. Voilà! (Screenshot #2)

The good thing about this method is that you can make as extensible as you wish... validate each value with different criteria, modify the data, add business rules, discard non-compliant values (by checking the full number of complying values)... !

Have a nice day!

Francisco.

PS: I had prepared a couple more screenshots... but stackoverflow has decided that I am too new to the site to post things with images or more than two links (!) Oh well..

Upvotes: 2

Related Questions