How to create a dynamic IN query in SSIS 2008?

回眸只為那壹抹淺笑 提交于 2020-01-06 07:16:14

问题


I have a variable @csv which hold a comma separated value such as: -a -a,b -a,b,c

I need to pass it in a query in my OLE DB source in a data flow to create a query such as:

SELECT COUNT(1) FROM table WHERE col1 IN @csv

So if @csv="a,b" then internally it should resolve into

SELECT COUNT(1) FROM table WHERE col1 IN 'a','b'

How can this be best achieved in SSIS 2008? Can I avoid the script component to create a dynamic query and storing it in a variable?


回答1:


How can this be best achieved in SSIS 2008? Can I avoid the script component to create a dynamic query and storing it in a variable?

The easiest/best way would still be with a script component.

Otherwise you could:

  1. use the csv as data source and select your result
  2. use the and "add column" tool to add the rest of your SQL query around the result
  3. store the result into a variable
  4. Then use a the OLE DB datasource with "query from variable"



回答2:


You can create a variable to store the query and compose its value using an expression, like:

List of Variables:

Option 1: In case of using OLE DB, select SQL Command from variable and bind the variable @sqlQuery:

Option 2: In case of using ADO.NET, Go to properties of Data Flow Task and expand Expressions and bind the ADO.NET Source > SqlCommand with the variable @sqlQuery + Make sure that ADO.NET Source > Data access mode is a SQL Command:

Option 3: In case of using Execute SQL Task, expand Expressions and bind the SqlStatementSource with the variable @sqlQuery + Make sure that SQL Source Type is a Direct Input:



来源:https://stackoverflow.com/questions/3869887/how-to-create-a-dynamic-in-query-in-ssis-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!