SSIS For Loop Container with Date Variable

◇◆丶佛笑我妖孽 提交于 2020-03-03 05:03:25

问题


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:

  1. 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())

  1. Declare variable Counter(datetime)

  2. Create a For loop container as follows :

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!