问题
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:
- use the csv as data source and select your result
- use the and "add column" tool to add the rest of your SQL query around the result
- store the result into a variable
- 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