I have two tables table1
and table2
, table2
have hierarchical data like below
geo_id parent_id
1 NULL
2 1
3 2
4 3
5 3
6 3
and table1 has data like below
ID geo_id
1 2
2 3
3 3
4 3
5 5
6 5
finally I have below Update trigger,
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[updateGeoHierarchyChilds] ON [dbo].[table2]
FOR UPDATE
AS
declare @ID int
BEGIN
Declare VicIds CURSOR LOCAL FOR SELECT geo_id from inserted
OPEN VicIds
FETCH NEXT FROM VicIds into @ID
WHILE @@FETCH_STATUS=0
BEGIN
update table1
set anyCol=AnyValue // suppose setting any column
where geo_id=@ID
update table2
set anyCol=AnyValue // suppose setting any column
where parent_id=@ID
FETCH NEXT FROM VicIds INTO @ID
END
CLOSE VicIds
DEALLOCATE VicIds
END
when I update table2 for example, I run below command stand alone
update table2
set anyCol=anyValue //this line is supposition for sake of example
where geo_id=2
it updates table1
for geo_id=2
it also updates all child where parent_id=2
in table2
but it not updating table1
against childs of geo_id=2
simply saying, When below portion of trigger called
update table2
set anyCol=AnyValue // suppose setting any column
where parent_id=@ID
it updates bunch of child records in same table, but child records not updating thier relation records in table1
You trigger will not recurse till you set an option
ALTER DATABASE [you db] SET RECURSIVE_TRIGGERS ON WITH NO_WAIT
GO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.