触发器实现表A增加、删除、更新时表B也自动增加、删除、更新_Baby-boy_ ...

drop trigger after_add_users
drop trigger after_upd_users
drop trigger after_del_users
GO
--增加用户,同时账务表中增加一条空记录
CREATE TRIGGER after_add_users
   ON users
   AFTER insert
AS
BEGIN
declare @newpk int
declare @username varchar(50)
declare @aname varchar(50)
declare @belong nchar(10)
declare @ctrl nchar(10)
set @newpk = (select top 1 userid from inserted)
set @username = (select top 1 username from inserted)
set @aname = (select top 1 aname from inserted)
set @belong=(select top 1 belong from inserted)
set @ctrl=(select top 1 ctrl from inserted)

insert manage (userid, username, aname,belong,ctrl) values(@newpk, @username, @aname,@belong,@ctrl)
END
GO

--修改用户,同时修改账务表中的用户名及别名
CREATE TRIGGER after_upd_users
   ON users
   AFTER UPDATE
AS
BEGIN
declare @newpk int
declare @username varchar(50)
declare @aname varchar(50)
declare @belong nchar(10)
declare @ctrl nchar(10)
set @newpk = (select top 1 userid from inserted)
set @username = (select top 1 username from inserted)
set @aname = (select top 1 aname from inserted)
set @belong=(select top 1 belong from inserted)
set @ctrl=(select top 1 ctrl from inserted)

update manage
set username = @username, aname = @aname, ,
where userid = @newpk
END
GO

--删除用户,同时删除账务数据
CREATE TRIGGER after_del_users
   ON users
   AFTER DELETE
AS
BEGIN
declare @newpk int
set @newpk = (select top 1 userid from deleted)

delete from manage where userid = @newpk
END
GO

记住必须两个表中都有相同的字段,可以看上面的做相应的改变



郑重声明:资讯 【触发器实现表A增加、删除、更新时表B也自动增加、删除、更新_Baby-boy_ ...】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——