2014-10-28 71 views
1

我希望你能幫助我!我非常感謝來自這裏的幫助!在SSIS包中設置動態變量日

我想要做的是創建SSIS包來導入.TXT分隔文件到數據庫中。用靜態文件做這件事的過程在我的腦海中是清楚的,但是這裏的事情是,必須每天使用來自昨天的文件來完成導入。 文件名具有以下結構:「Informe_De_Recupero_D141027」(日期部分爲「141027」)。

我創建了一個變量在SSIS包稱爲FileDateName以下表達式:

RIGHT("00" + (DT_STR, 4 , 1252)DATEPART("year" , GETDATE()),2) + "" 
+ RIGHT("00" + (DT_STR, 2 , 1252)DATEPART("month" , GETDATE()) , 2) 
+ "" + (DT_STR, 2 , 1252) DATEPART("day" , getdate()-1) 

但-1不在這種情況下工作。它導致以下錯誤:

The data type "DT_DBTIMESTAMP" cannot be used with binary operator "-" the type of one or both of the operands is not supported for the operation. To perform this operation, one or both operands needs to be explicity cast with a cast operator.

Attempt to set the result type of binary operation "Getdate()-1" failed with error code 0xc0047081 Evaluating function "DATEPART" failed with error code 0xc0047084

我該如何解決它?你可以給我另一種解決方案嗎?

在此先感謝!

回答

1

SSIS表達式語言不支持getdate() - 1。您將不得不使用DATEADD(),即

+ (DT_STR, 2 , 1252) DATEPART("day" , Dateadd("day",-1,getdate())) 

此外,這應該爲每個日期部分完成。什麼時候你到了今年年初發生 - 141231,將轉化爲150131.

最後,在執行SQL任務,這樣會更容易:

Select Right(Convert(char(8), getdate()-1,112),6) 
+0

完美!非常感謝! – Marcos 2014-10-28 16:51:10