SSIS flow ADO.net source task with parameter

2019-02-26 13:14发布

问题:

I am using a ADO.NET source to bring data from an ODBC server to the my SQL. I used the import and export wizard to create my data flow task.

I need to append new records that have field GPMTADDT>(yesterday date) for example if today 20121002 the following query works perfectly:

SELECT  PARTICIP.* 
FROM PARTICIP
WHERE GPMTADDT > 20121001

I tried:

SELECT  PARTICIP.* 
FROM PARTICIP
WHERE GPMTADDT > Format(now(),"yyyymmdd")

But it will not work, the server treats "format" as a column. I tried creating a variable @date

SELECT  PARTICIP.* 
FROM PARTICIP
WHERE GPMTADDT > @date

again the server rejected the "@".

Also tried:
SELECT  PARTICIP.* 
FROM PARTICIP
WHERE GPMTADDT > (SELECT MAX PARTICIP.GPMTADDT FROM PARTICIP)

I am sure I am missing something simple, help will be greatly appreciated.

Thank you!

回答1:

Have you tried:

SELECT PARTICIP.*
FROM PARTICIP
WHERE GPMTADDT > DATEADD(dd, -1, GETDATE())


回答2:

You haven't mentioned what your data source actually is, but there appear to be several errors in the SQL syntax you've tried. e.g. the first query should have single quotes around the date, the last one should put the argument to MAX() in parentheses etc.

But to try to answer your basic question, ADO.NET sources do not support parameters, unlike OLE DB ones that do. Some possible solutions are:

  1. Don't use a parameter if you can generate the value in the source itself; this is what you are trying to do in your last example and what Jeff has suggested
  2. Use an SSIS expression to set the SqlCommand property of the connection object, together with an SSIS variable (as described here)
  3. As an alternative to #2, use a Script task to build the complete SELECT query, assign it to an SSIS variable, and then use that variable as the SqlCommand value

Personally I would say that option 1 is the easiest but of course not always possible, if the variable isn't accessible from within the source system. 2 and 3 are variations on the same solution, but I prefer 3 because I find writing a script easier than working with expressions.