2012-02-29 64 views
0

已經構建了一個基本上查看包含所有銀行假期的表並查看當前財政年度的每個月的查詢,然後告訴我有多少工作日可用於工作減去銀行休息室和週末。例如這個月有21個。還有一個累積的提交每月基本累計,所以4 - 2月的累積將會增加所有這些日子。包括創建臨時表的SQL的SSIS作業

在查詢中有一個CreateTable #DATA在最後被刪除,這在Management Studio中正常工作並正常運行。

我的問題是我正在做一個SSIS工作,並保存我的查詢作爲一個SQL文件,並選擇它使用「瀏覽」按鈕。它不允許我繼續,因爲我認爲它有臨時表的問題(見屏幕截圖)

有關如何在保持功能的同時使其工作的任何建議?

請參閱代碼以供參考:

DECLARE @StartDate DATETIME, 
     @EndDate DATETIME 



SELECT @StartDate = (select 
case when month(getdate()) >= 4 then 
convert(datetime, cast(year(getdate()) as varchar) + '-4-1') 
else 
convert(datetime, cast(year(getdate())-1 as varchar) + '-4-1') 
end), 



     @EndDate = (select 
case when month(getdate()) < 4 then 
convert(datetime, cast(year(getdate()) as varchar) + '-3-31') 
else 
convert(datetime, cast(year(getdate())+1 as varchar) + '-3-31') 
end) 

CREATE TABLE #data 
    ( 
    firstday DATETIME NOT NULL PRIMARY KEY, 
    workingdays INT NOT NULL 
); 

WITH dayscte ([Date]) 
    AS (SELECT @StartDate 
     UNION ALL 
     SELECT Dateadd(DAY, 1, [Date]) 
     FROM dayscte 
     WHERE [Date] <= @Enddate) 
INSERT INTO #data 
SELECT MIN([Date]), 
     COUNT(*) [Day] 


FROM table2 
     LEFT JOIN [dbo].[mydb].[mytable1] 
     ON [Date] BETWEEN [dbo].[mydb].[mytable1].startdate AND [dbo].[mydb].[mytable1].enddate 



where 
NOT EXISTS (
    SELECT field1,field2 FROM [dbo].[mydb].[mytable1].tscheme_cal WHERE 
    dayid ='0234572347854234' 
    AND 
    [date] <= startdate 
    AND 
    [date] >= enddate 
    ) 
     AND Datename(weekday, [Date]) NOT IN ('Saturday', 'Sunday') 
GROUP BY Datepart(MONTH, [Date]), 
      Datepart(YEAR, [Date]) 
OPTION (MAXRECURSION 366) 

DECLARE @Date DATETIME 

SET @Date = (SELECT MIN(firstday) 
      FROM #data) 

SELECT Period, 
     workingdays [Days_Available] , 

     year (firstday) AS [Year] 


FROM (SELECT Datename(MONTH, firstday) [Period], 
       workingdays, 
       0       [SortField], 
       firstday 
     FROM #data 
     UNION 
     SELECT Datename(MONTH, @Date) + ' - ' + Datename(MONTH, firstday), 
       (SELECT SUM(workingdays) 
       FROM #data b 
       WHERE b.firstday <= a.firstday) [WorkingDays], 
       1         [SortField], 
       firstday 
     FROM #data a 
     WHERE 


     firstday > @Date) data 





ORDER BY sortfield, 
      firstday 

DROP TABLE #data 

enter image description here

+0

您是否嘗試過 '聲明@data表'? – ChrisBD 2012-02-29 11:44:23

+0

還檢查出我對這個類似問題的答案http://stackoverflow.com/questions/18346484/ssis-package-not-wanting-to-fetch-metadata-of-temporary-table/18360265#18360265 – 2014-12-03 21:28:05

回答

1

其不容易對付的SSIS臨時表。 我建議這篇文章:

http://www.sqllike.com/using-temporary-tables-with-ssis.html 

它是一個解決方案,但我不喜歡它。 我有時候會使用表變量或者在數據庫上創建一個常規表,然後在最後刪除它。

1

在SSIS中,我發現表變量運行良好。如果它是轉換的源代碼,則即使在存儲過程中也不能使用臨時表。

+0

你可以一樣長因爲您在實際查詢之前聲明瞭一個虛擬結果集。另請參閱:http://stackoverflow.com/questions/18346484/ssis-package-not-wanting-to-fetch-metadata-of-temporary-table/18360265#18360265 – 2014-12-03 21:28:54

0

在SQL Server 2012中,如果使用臨時表,則必須指定一個結果集。

這是SSIS用來返回輸出元數據的sp_describe_first_result_set過程的問題。

E.g.

EXEC dbo.RptResults_StoredProcedure 

變爲

EXEC dbo.RptResults_StoredProcedure 
WITH RESULT SETS 
((
    Date NVARCHAR(10), 
    Location VARCHAR(12), 
    Department CHAR(1), 
    Shift CHAR(1), 
    ForecastSales DECIMAL(18,2), 
    ActualSales DECIMAL(18,2) 
)) 

欲瞭解更多信息視圖

http://blog.concentra.co.uk/2014/08/22/column-metadata-determined-correctly-ssis-data-flow-task-stored-procedure-inputs/

http://blog.concentra.co.uk/2014/08/22/column-metadata-determined-correctly-ssis-data-flow-task-stored-procedure-inputs/