我有一个名为ADSCHL
的表,其中包含school_code
作为主键
以及其他两个表格RGDEGR
(公共字段为SCHOOl_code
)和RGENRl(Original_school_code
)正在逐步提升ADSCHL
。
如果要更新school_code
,则表格RGDEGR (school_code)
和RGERNL ( original_schoolcode)
也必须同时更新。
我已经提供了一个新数据,我使用SSIS将其导入SQL服务器,表名为TESTCEP
,其列名为school_code
。我被分配了一个任务,用新的school_code
(ADSCHL
)更新旧的school_code
值(TESTCEP
),并确保所有3个表都发生了更改。
有人可以帮帮我吗?
我尝试使用Merge Update
功能,但无法执行此操作。它错了。
我尝试使用此查询,并不确定这是否会帮助我更新所有表格:
UPDATE dbo.ADSCHL
SET dbo.ADSCHL.SCHOOL_CODE = FD.SCHOOL_Code
FROM dbo.ADSCHL AD
INNER JOIN TESTCEP FD
ON AD.SCHOOL_NAME = FD.School_Name
答案 0 :(得分:0)
据我所知,您无法在单个语句中更新一个表。但是,在dbo.ADSCHL
更新之前,您可以编写一个而不是更新触发器来更新其他两个表:
更新触发前:
CREATE TRIGGER dbo.UpdateSchoolCode ON dbo.ADSCHL
INSTEAD OF UPDATE
AS
BEGIN
--update RGDEGR
UPDATE rd
SET rd.school_code = i.school_code
FROM RGDEGR rd
INNER JOIN deleted d ON d.school_code = rd.school_code
INNER JOIN inserted i ON i.id = d.id; --primary key join
--update RGENRl
UPDATE rg
SET rg.school_code = i.school_code
FROM RGENRl rg
INNER JOIN deleted d ON d.school_code = rg.school_code
INNER JOIN inserted i ON i.id = d.id; --primary key join
END;
然后在创建触发器后运行此更新语句:
--update ADSCHL
UPDATE ad
SET ad.SCHOOL_CODE = fd.SCHOOL_Code
FROM dbo.ADSCHL ad
INNER JOIN TESTCEP fd ON ad.SCHOOL_NAME = fd.School_Name;
注意:我为上面的评论道歉,我刚才意识到你需要一个更新之前的触发器而不是更新后的触发器。