2008-12-16

GETDATE in SQL SSIS Packages


Using things like SQL GETDATE() functions in SSIS Packages doesn't work (similar issue in user-defined functions).  It will always use the date value that was present when you first save the package, which is not very useful.  For example:

SELECT orders FROM TABLE WHERE orderdate >= DATEADD(m,-1, GETDATE())

would get you orders from the last month.  However, to make that work in an SSIS Package, here's a simple fix, though probably not the best from a performance standpoint:

Create a view names something like ExportDate with a function like this:

SELECT     GETDATE() AS DT

Then, update your query to be:

SELECT orders FROM TABLE WHERE orderdate >= DATEADD(m,-1, (SELECT DT FROM dbo.vwECR_ExportDate))

The view will update when it's queried, so you're all set.

And no, apparently you can't edit SSIS packages you've saved to the server, at least not with SQL Management Studio.

No comments: