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
记住必须两个表中都有相同的字段,可以看上面的做相应的改变