我知道我会为此发火,但是......无限触发循环....按设计(!)。如何解决?
我有表ProductA,ProductB和ProductC,它们具有非常相似的模式,但是每个表中有2或3列。每个表都有一个插入触发器,它将A,B或C中每个插入的重复行触发到表产品,这是所有产品的合并。另外,A,B或C上的更新触发器同样会更新Table Products中的等价行,删除触发器也是如此。所有的工作完美无瑕,直到.....我们更新,例如,表产品列A,它也存在于表A,B和C.
我正在寻找开发表的产品上的触发器,将传播在表A,B和C中的每一个表中,将列A中的列更新为BUT,而不调用表A,B和C上的更新触发器。期望的行为是更新在两个方向上工作而不产生无限循环(注意,在表产品只有2列需要回到复制到表A,B和C)
选项包括:
- 重新设计架构,以便这种情况并不存在(N在 卡中,这是一个快速解决方案,重新设计可以由别人完成 );
- 当我更新表产品 时,手动禁用触发器(这全部在应用程序级完成,用户在更新表 产品时没有 登录到SSMA并禁用触发器的功能);
- 来堆栈溢出,并希望有人已经遇到这种类型的问题!
从概念上讲,这怎么可以做到?
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名称变更为保持与发布文字)
假设你必须保留触发器,我可以想象一个涉及每个表的'rowversion'的解决方案,也存储在其他表的每个表中。 – Oded
更新是否需要实时?如果没有,那么一个简单的存储过程来同步表格将是一个更简单的解决方案。您可以按计划进行设置。 –
看看[TRIGGER_NESTLEVEL](http://msdn.microsoft.com/en-us/library/ms182737.aspx?ppud=4)。 – HABO