我知道我会因此而发火,但是......
我有表 ProductA、ProductB 和 ProductC,它们的架构非常相似,但每个都有 2 或 3 列。每个表都有一个插入触发器,它为 A、B 或 C 中的每个插入触发重复行到表 Products,这是所有产品的合并。此外,A、B 或 C 上的更新触发器也将更新它们在表产品中的等效行,删除触发器也是如此。所有工作都完美无缺,直到......我们更新了表格产品列 A,它也存在于表 A、B 和 C 中。
我希望在表产品上开发一个触发器,它将 A 列中的更新传播到每个表 A、B 和 C 中的 A 列,但是,而不调用表 A、B 和 C 上的更新触发器。期望的行为是更新在两个方向上工作而不会产生无限循环。(注意,表产品中只有 2 列需要复制回表 A、B 和 C)
选项是:
- 重新设计架构,使这种情况不存在(不在卡片中,这是一个快速的解决方案,重新设计可以由其他人完成);
- 更新表产品时手动禁用触发器(这都是在应用程序级别完成的,用户在更新表产品时将无法登录 SSMA 并禁用触发器);
- 来到 Stack Overflow,希望有人已经遇到过此类问题!
从概念上讲,如何做到这一点?
6/7 更新:
这是表 A 上的触发代码(例如):
ALTER TRIGGER [dbo].[GRSM_WETLANDS_Point_GIS_tbl_locations_update]
ON [dbo].[GRSM_WETLANDS_POINT]
after update
AS
BEGIN
SET NOCOUNT ON;
update dbo.TBL_LOCATIONS
set
X_Coord = i.X_Coord,
Y_Coord = i.Y_Coord,
PlaceName = i.PlaceName,
FCSubtype = case
when i.FCSubtype = 1 then 'Point: Too Small to Determin Boundary'
when i.FCSubtype = 2 then 'Point: Boundary Determined by Contractor but not Surveyed'
when i.FCSubtype = 3 then 'Point: Wetland Reported but not yet Surveyed'
end ,
Landform = i.Landform
from dbo.TBL_LOCATIONS
Join inserted i
on TBL_LOCATIONS.GIS_Location_ID = i.GIS_Location_ID
end
GO
和
ALTER TRIGGER [dbo].[GRSM_WETLANDS_POINT_GIS_tbl_locations]
ON
[dbo].[GRSM_WETLANDS_POINT]
after INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.TBL_LOCATIONS(
X_Coord, Y_Coord,
PlaceName,
FCSubtype, Landform
)
SELECT
a.X_Coord, a.Y_Coord,
a.PlaceName,
a.FCSubtype, a.Landform
From
(
SELECT
X_Coord, Y_Coord,
PlaceName,
FCSubtype = case
when FCSubtype = 1 then 'Point: Too Small to Determin Boundary'
when FCSubtype = 2 then 'Point: Boundary Determined by Contractor but not Surveyed'
when FCSubtype = 3 then 'Point: Wetland Reported but not yet Surveyed'
end ,
Landform
FROM inserted
) AS a
end
GO
这是当前在表产品上禁用的更新触发器:
ALTER TRIGGER [dbo].[tbl_locations_updateto_geo]
ON [dbo].[TBL_LOCATIONS]
for update
AS
BEGIN
--IF @@NESTLEVEL>1 RETURN
SET NOCOUNT ON;
update dbo.GRSM_Wetlands_Point
set
X_Coord = i.X_Coord,
Y_Coord = i.Y_Coord,
PlaceName = i.PlaceName,
FCSubtype = i.FCSubtype,
Landform = i.Landform,
from dbo.TBL_LOCATIONS
Join inserted i
on TBL_LOCATIONS.GIS_Location_ID = i.GIS_Location_ID
where TBL_LOCATIONS.FCSubtype = 'Polygon: Determination Made by GPS Survey'
or TBL_LOCATIONS.FCSubtype = 'Polygon: Determination Derived from NWI'
or TBL_LOCATIONS.FCSubtype = 'Polygon: Determination Made by Other Means'
or TBL_LOCATIONS.FCSubtype = 'Polygon: Legal Jurisdictional Determination';
end
GO
(tbl 名称已更改以与发布文本保持一致)