简体   繁体   English

SSIS执行sql任务参数映射

[英]SSIS execute sql task parameter mapping

I'm trying to execute a sql script using the task in SSIS.我正在尝试使用 SSIS 中的任务执行 sql 脚本。

My script just inserts a bunch of nambe value pairs in a table.我的脚本只是在表中插入了一堆 nambe 值对。 For example -例如 -

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

Now, I want to map a variable defined in SSIS to be mapped to the parameters in the statement above.现在,我想将 map 中定义的一个变量 SSIS 映射到上面语句中的参数。 I tried defining a scalar variable but I guess the sql task doesn't like that.我尝试定义一个标量变量,但我猜 sql 任务不喜欢那样。 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')例如我想插入到 mytable (name, value) 值 ('name1', 'value1'), ('name1', 'value2')

When I open the Parameter Mapping tab for the task, it wants me to map each parameter invidually like -当我打开任务的参数映射选项卡时,它希望我将 map 每个参数单独设置为 -

Variable Name - User::Name Direction - Input Data Type - LONG Parameter Name - 0 Parameter Size - -1变量名称 - User::Name 方向 - 输入数据类型 - LONG 参数名称 - 0参数大小 - -1

Variable Name - User::Name Direction - Input Data Type - LONG Parameter Name - 1 Parameter Size - -1变量名称 - User::Name 方向 - 输入数据类型 - LONG 参数名称 - 1参数大小 - -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.如果一个名称有 5-10 个值,这很快就会变得失控和繁琐,并迫使我为同一个名称添加多个分配。

Is there an easy(-ier) way to do this?有没有简单(-ier)的方法来做到这一点?

The easiest (and most extensible) way, is to use a Data Flow Task instead of using an Execute SQL Task.最简单(也是最可扩展)的方法是使用数据流任务而不是使用执行 SQL 任务。

  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).创建一个包含您需要插入的列的虚拟行,因此请使用您最喜欢的任何内容作为源(在此示例中,我使用了 oledb 连接)。 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 ).这会将数据流的元数据与插入表(屏幕截图#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添加一个 union all 并加入所有流
  7. Insert into the table插入表格
  8. Voilà!瞧! ( Screenshot #2 ) 截图#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.. PS:我准备了更多的屏幕截图......但是stackoverflow决定我对这个网站太陌生了,不能发布带有图片或两个以上链接的东西(。)哦,好吧......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM