我有一個每星期一運行一週的SSIS工作。目前,作業從電子表格中提取數據,通過聚合運行它(因爲我只需要每年SUM),然後將數據放入臨時表[Staging.HRIS_RecruitingGL]
。更新聲明?
兩張表的結構完全相同。
TABLE [dbo].[HRIS_RecruitingGL](
[FiscalYear] [varchar](50) NOT NULL,
[Amount] [decimal](20, 2) NOT NULL)
處於暫存表中的數據如下所示。
|FiscalYear|Amount|
|2012 |250.25|
|2013 |175.13|
由於此報告每週運行一次,因此我需要將當前年份(以及隨後的幾年)更新爲該作業。因此,我需要一個腳本,它將從暫存表中提取數據並更新我的主表[dbo.HRIS_RecruitingGL]
上的年度數量。隨着時間的推移,主表將隨着年齡的增長而增長。
由於每次作業運行時都會截斷暫存表,我不能直接將數據直接加載到主表中。從週一開始,我將收到的數據僅用於當年(和未來幾年),他們刪除了2012年的數據。 但我需要保留它在我的表中,所以截斷主表是不是一個選項(這是我的原始方法,截斷表和加載新數據,非常簡單)
這是合併聲明,我試圖使用。
MERGE dbo.HRIS_RecruitingGL AS tgt
USING (
SELECT DATENAME(YEAR, GETDATE()) AS FiscleYear AND Amount,
FROM Staging.HRIS_RecruitingGL
) AS rgl ON rgl.FiscalYear = tgt.FiscalYear
WHEN MATCHED
THEN UPDATE
SET tgt.FiscalYear = rgl.FiscalYear,
tgt.Amount = rgl.Amount
WHEN NOT MATCHED BY TARGET
THEN INSERT (
FiscalYear,
Amount
)
VALUES (
rgl.FiscalYear,
rgl,Amount
);
會是什麼,我可以使用,將簡單地更新從臨時表的電流年金額,並添加一個新行明年啓動時和更新信息,以及腳本?
感謝您提供任何幫助。
更新: 我按照您的建議更改了腳本,並收到以下語法錯誤。
Msg 207, Level 16, State 1, Line 3
Invalid column name 'FiscleYear'.
Msg 207, Level 16, State 1, Line 7
Invalid column name 'FiscalYear'.
我添加了一個屏幕快照,以便您可以看到表格的設置。
UPDATE:
我添加腳本到SQL任務在我的SSIS包。當我運行該作業時,它返回了以下錯誤消息。
[Execute SQL Task] Error: Executing the query "MERGE dbo.HRIS_RecruitingGL AS tgt
USING (..." failed with the following error: "The MERGE statement attempted to
UPDATE or DELETE the same row more than once. This happens when a target row matches
more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the
target table multiple times. Refine the ON clause to ensure a target row matches at
most one source row, or use the GROUP BY clause to group the source rows.". Possible
failure reasons: Problems with the query, "ResultSet" property not set correctly,
parameters not set correctly, or connection not established correctly.
你能後的'dbo.HRIS_RecruitingGL'表的結構,包括數據類型和約束。 –
在您從暫存表中選擇時,您不包括'Amount'字段。 –
我在最後添加了AND數量,它仍然給我一個「FROM字附近的語法錯誤」錯誤。 –