问题
I want to create a monthly package that executes a daily query at ODBC and writes an output file.
More specifically the query must be first executed for the first day of the previous month (e.g. '01/11/2018')
then the next one ('02/11/2018')
until the last day of the previous month ('30/11/2018')
.
The date variables are currently saved as Strings and I also want to have a string variable with Oracle date format to be inserted into the query. How should it be organised? Is there a way that I could use the string variables in the expressions?
回答1:
Break it into parts as follows:
- Declare variables to store previous month start and end date as follows:
start_date(datetime) = (DT_DATE)((DT_WSTR,4)YEAR(DATEADD("MM",-1,GETDATE()))+"-"+RIGHT("0"+(DT_WSTR,2)MONTH(DATEADD("MM",-1,GETDATE())),2)+"-01")
end_date(datetime) = DATEADD("D", -(DAY(GETDATE())),GETDATE())
Declare variable
Counter(datetime)
Create a
For loop container
as follows :
- Rest of the Data Flow Task should be there within For loop container, which will create output file. You can use the variable
Counter
in SQL to parameterize it
回答2:
In fact, I figured out that all I wanted to use in my loop was the daypart of the date, so I created two extra int variables that contains:
1) the first day of the month (1)
2) the last day of the month (28,30,31)
I used those two variables at the For Loop Expressions
and convert the index to string, so I could add it in the query. Possibly there will be a better way and it would be welcome.
来源:https://stackoverflow.com/questions/53591179/ssis-for-loop-container-with-date-variable