Is it possible to override the value of a variable in a SSIS package, which is calculated via an expression, by passing in a different value on the DTExec command line?
I have a parameterized query and I'm passing variable User::StartDate
into it. The package name is OpenAirExport.dtsx.
If I invoke my package using DTExec from PowerShell I can set the StartDate variable fine on the command line. For example:
& "C:\Program Files\Microsoft SQL Server\150\DTS\Binn\DTExec.exe" -File 'OpenAirExport.dtsx' `
-Set '\Package.Variables[User::StartDate].Properties[Value];2020-09-22'
This works fine. I can see from the resulting data that the start date was indeed set to 22 Sep 2020.
99% of the time, though, the package will just be getting the previous day's data. So I set an expression on the StartDate variable to calculate its value:
DATEADD("day", DATEDIFF("day", (DT_DBTIMESTAMP)0, GETDATE()) - 1, (DT_DBTIMESTAMP)0)
(This rather complicated looking expression just counts the number of days since day 0 then adds them to day 0 and subtracts 1. This is a fairly standard way in SQL Server of stripping the time from a datetime, leaving just the date. In this case it will give a datetime of 00:00 hours yesterday)
Now if I call the package using DTExec from PowerShell without setting the StartDate variable value it gives the correct result - data from yesterday.
Every now and again a downstream process fails and we're asked to re-run a previous day's data. So I want to be able to override the calculated value for StartDate with a value passed in from the command line. But I find that if I try to set the value from the command line this value gets ignored and the expression is still used to calculate StartDate.
Is there any way I can force SSIS to override the expression value with a value passed in from the command line?
You are overriding it. The problem is, that en expression is evaluated every time you read/use it so the package starts up, you specify what the value should be and then when a task/component goes to use the value, the execution engine says "Oh, StartDate has an expression on it. Let's evaluate it in case the value has changed"
A easier option would be to remove the expression altogether from @StartDate. Set to the minimum (or maximum) date value which should never do anything if the package is run outside of the standard mechanism of your Posh script. With the fail safe in place, you have your standard start of
where $RunDate was calculated beforehand.
When the downstream system fails, you explicitly change the value and patch in the assignment so it gets used.