如何让触发器级联修改字段值?

csz_1987 2011-08-02 01:59:32


做了一个无限层级结构。
如何修改其父部门,让触发器级联修改所有子部门。
例如:修改了‘福建省’的IsForbid字段,让触发器级联更新‘福建省’下的所有市县的IsForbid字段,
让‘子’与‘父’的IsForbid值相同。

我写了一个触发器,但是他只会触发一次。例如,我修改了‘福建省’的IsForbid值,触发器只会更新‘市’一级的节点,
而‘市’一级下面的‘区’、‘县’却无法触发。


CREATE TRIGGER tr_DeptBase ON DeptBase
FOR UPDATE
AS
BEGIN
DECLARE @DeptID INT ;
DECLARE @IsForbid BIT ;
SELECT @DeptID = DeptID ,
@IsForbid = IsForbid
FROM INSERTED ;

UPDATE DeptBase
SET IsForbid = @IsForbid
WHERE BaseDeptID = @DeptID ;
END


这种需求要如何实现?



...全文
111 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
csz_1987 2011-08-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 acherat 的回复:]

楼主可以看看05使用cte递归的例子或资料。
[/Quote]

谢了。。。
AcHerat 元老 2011-08-02
  • 打赏
  • 举报
回复
楼主可以看看05使用cte递归的例子或资料。
csz_1987 2011-08-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
SQL code

--2005

create trigger tname_up on DeptBase
for update
as
;with ach as
(
select DeptID,BaseDeptID,DeptName,DeptText,DeptTel,IsForbid from inserted
union all
select a.DeptID,a.Ba……
[/Quote]

但是有点看不懂。能不能帮忙解释下?
csz_1987 2011-08-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]

SQL code

--2005

create trigger tname_up on DeptBase
for update
as
;with ach as
(
select DeptID,BaseDeptID,DeptName,DeptText,DeptTel,IsForbid from inserted
union all
select a.DeptID,a.B……
[/Quote]

太感谢了。
AcHerat 元老 2011-08-02
  • 打赏
  • 举报
回复

--2005

create trigger tname_up on DeptBase
for update
as
;with ach as
(
select DeptID,BaseDeptID,DeptName,DeptText,DeptTel,IsForbid from inserted
union all
select a.DeptID,a.BaseDeptID,b.DeptName,b.DeptText,b.DeptTel,b.IsForbid
from DeptBase a join ach b on a.BaseDeptID = b.DeptID
)
update a
set a.DeptText = b.DeptText,a.DeptTel = b.DeptTel,a.IsForbid = b.IsForbid
from DeptBase a,ach b
where a.DeptID = b.DeptID
go

update DeptBase
set IsForbid = 1
where DeptID = 26

select *
from DeptBase

drop trigger tname_up


/************

DeptID BaseDeptID DeptName DeptText DeptTel IsForbid
----------- ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------
26 NULL Dept_26 福建省 可空 1
27 NULL Dept_27 浙江省 可空 0
28 NULL Dept_28 广东省 可空 0
29 NULL Dept_29 广西省 可空 0
30 NULL Dept_30 云南省 可空 0
31 26 Dept_31 福建省 可空 1
32 26 Dept_32 福建省 可空 1
33 26 Dept_33 福建省 可空 1
34 26 Dept_34 福建省 可空 1
35 26 Dept_35 福建省 可空 1
....
csz_1987 2011-08-02
  • 打赏
  • 举报
回复
如何对自关联的表进行级联更新操作啊?
AcHerat 元老 2011-08-02
  • 打赏
  • 举报
回复

--2000

create function get_P(@id int)
returns varchar(4000)
as
begin
declare @ret varchar(400)
declare @level int
declare @tb table
(
DeptID int,
BaseDeptID int,
lev int
)
set @level = 1
insert into @tb
select DeptID,BaseDeptID,@level from DeptBase where DeptID = @id
while(@@rowcount>0)
begin
set @level = @level + 1
insert into @tb
select a.DeptID,a.BaseDeptID,@level
from DeptBase a join @tb b on a.BaseDeptID = b.DeptID and lev = @level - 1
end
select @ret = isnull(@ret+',','') + ltrim(DeptID) from @tb
return @ret
end
go

create trigger tname_up on DeptBase
for update
as
update a
set a.DeptText = b.DeptText,a.DeptTel = b.DeptTel,a.IsForbid = b.IsForbid
from DeptBase a,inserted b
where charindex(','+ltrim(a.DeptID)+',',','+dbo.get_P(b.DeptID)+',') > 0
go

update DeptBase
set IsForbid = 1
where DeptID = 26

select *
from DeptBase
where charindex(','+ltrim(DeptID)+',',','+dbo.get_P(26)+',') > 0

drop trigger tname_up
drop function get_P

/************

DeptID BaseDeptID DeptName DeptText DeptTel IsForbid
----------- ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------
26 NULL Dept_26 福建省 可空 1
31 26 Dept_31 福建省 可空 1
32 26 Dept_32 福建省 可空 1
33 26 Dept_33 福建省 可空 1
34 26 Dept_34 福建省 可空 1
35 26 Dept_35 福建省 可空 1
36 26 Dept_36 福建省 可空 1
37 26 Dept_37 福建省 可空 1
38 26 Dept_38 福建省 可空 1
39 26 Dept_39 福建省 可空 1
40 31 Dept_40 福建省 可空 1
41 31 Dept_41 福建省 可空 1
42 31 Dept_42 福建省 可空 1
43 31 Dept_43 福建省 可空 1
44 31 Dept_44 福建省 可空 1
45 31 Dept_45 福建省 可空 1
46 31 Dept_46 福建省 可空 1
47 31 Dept_47 福建省 可空 1
48 31 Dept_48 福建省 可空 1
49 31 Dept_49 福建省 可空 1
50 31 Dept_50 福建省 可空 1
51 31 Dept_51 福建省 可空 1
52 32 Dept_52 福建省 可空 1
53 32 Dept_53 福建省 可空 1
54 32 Dept_54 福建省 可空 1
55 32 Dept_55 福建省 可空 1
56 32 Dept_56 福建省 可空 1
57 32 Dept_57 福建省 可空 1
58 33 Dept_58 福建省 可空 1
59 33 Dept_59 福建省 可空 1
60 33 Dept_60 福建省 可空 1
61 33 Dept_61 福建省 可空 1
... 1

(72 行受影响)
csz_1987 2011-08-02
  • 打赏
  • 举报
回复

--测试数据
SET IDENTITY_INSERT [DeptBase] ON
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (26,NULL,'Dept_26','福建省','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (27,NULL,'Dept_27','浙江省','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (28,NULL,'Dept_28','广东省','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (29,NULL,'Dept_29','广西省','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (30,NULL,'Dept_30','云南省','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (31,26,'Dept_31','福州市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (32,26,'Dept_32','厦门市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (33,26,'Dept_33','漳州市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (34,26,'Dept_34','泉州市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (35,26,'Dept_35','三明市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (36,26,'Dept_36','莆田市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (37,26,'Dept_37','南平市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (38,26,'Dept_38','龙岩市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (39,26,'Dept_39','宁德市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (40,31,'Dept_40','鼓楼区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (41,31,'Dept_41','台江区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (42,31,'Dept_42','仓山区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (43,31,'Dept_43','晋安区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (44,31,'Dept_44','马尾区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (45,31,'Dept_45','福清市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (46,31,'Dept_46','长乐市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (47,31,'Dept_47','闽侯县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (48,31,'Dept_48','平潭县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (49,31,'Dept_49','闽清县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (50,31,'Dept_50','连江县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (51,31,'Dept_51','罗源县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (52,32,'Dept_52','思明区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (53,32,'Dept_53','湖里区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (54,32,'Dept_54','集美区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (55,32,'Dept_55','海沧区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (56,32,'Dept_56','同安区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (57,32,'Dept_57','翔安区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (58,33,'Dept_58','芗城区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (59,33,'Dept_59','龙文区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (60,33,'Dept_60','龙海市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (61,33,'Dept_61','漳浦县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (62,33,'Dept_62','华安县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (63,33,'Dept_63','东山县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (64,33,'Dept_64','长泰县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (65,33,'Dept_65','云霄县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (66,33,'Dept_66','南靖县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (67,33,'Dept_67','平和县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (68,33,'Dept_68','诏安县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (69,34,'Dept_69','洛江区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (70,34,'Dept_70','泉港区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (71,34,'Dept_71','晋江市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (72,34,'Dept_72','石狮市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (73,34,'Dept_73','惠安县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (74,34,'Dept_74','安溪县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (75,34,'Dept_75','德化县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (76,34,'Dept_76','永春县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (77,34,'Dept_77','南安市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (79,35,'Dept_79','三元区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (80,35,'Dept_80','梅列区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (81,35,'Dept_81','永安市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (82,35,'Dept_82','清流县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (83,35,'Dept_83','宁化县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (84,35,'Dept_84','建宁县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (85,35,'Dept_85','泰宁县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (86,35,'Dept_86','明溪县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (87,35,'Dept_87','将乐县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (88,35,'Dept_88','沙县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (89,35,'Dept_89','尤溪县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (90,35,'Dept_90','大田县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (93,36,'Dept_93','城厢区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (94,36,'Dept_94','涵江区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (95,36,'Dept_95','秀屿区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (96,37,'Dept_96','延平区','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (97,37,'Dept_97','邵武市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (99,37,'Dept_99','武夷山','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (100,37,'Dept_100','建瓯市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (101,37,'Dept_101','建阳市','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (102,37,'Dept_102','顺昌县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (103,37,'Dept_103','浦城县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (104,37,'Dept_104','光泽县','可空',0)
INSERT [DeptBase] ([DeptID],[BaseDeptID],[DeptName],[DeptText],[DeptTel],[IsForbid]) VALUES (105,37,'Dept_105','松溪县','可空',0)
SET IDENTITY_INSERT [DeptBase] OFF
csz_1987 2011-08-02
  • 打赏
  • 举报
回复

--创建表
/*==============================================================*/
/* Table: DeptBase */
/*==============================================================*/
create table DeptBase (
DeptID int identity,
BaseDeptID int null,
DeptName nvarchar(50) null,
DeptText nvarchar(50) not null,
DeptTel nvarchar(50) null,
IsForbid bit not null,
constraint PK_DEPTBASE primary key (DeptID)
)
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'部门表',
'user', @CurrentUser, 'table', 'DeptBase'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'部门ID',
'user', @CurrentUser, 'table', 'DeptBase', 'column', 'DeptID'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'上级部门ID',
'user', @CurrentUser, 'table', 'DeptBase', 'column', 'BaseDeptID'
go

declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'部门电话',
'user', @CurrentUser, 'table', 'DeptBase', 'column', 'DeptTel'
go





34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧