我有一个每星期一运行一周的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字附近的语法错误”错误。 –