CREATE TRIGGER [TRIGGER user] ON [dbo].[Netuser]
FOR INSERT
AS
declare @UserNo nvarchar(50)
declare @dep nvarchar(50)
declare @count int
select @UserNo=UserNo from inserted
select @dep=dep from PSWD,netuser where PSWD.USR = Netuser.UserNo and Netuser.UserNo=@UserNo
SELECT @count=COUNT(PSWD.DEP)
FROM PSWD INNER JOIN
Netuser ON PSWD.USR = Netuser.UserNo AND PSWD.DEP =@dep
GROUP BY DEP
if @dep='0000' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if (@dep='0001' or @dep='0003' or @dep='0004' or @dep='0010' or @dep='0011' or @dep='0020') and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0002' and @count>3
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0006' and @count>2
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0014' and @count>2
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0005' and @count>5
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0008' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0007' and @count>5
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0009' and @count>5
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0016' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0017' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0018' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0021' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0023' and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if (@dep='0025' or @dep='0026') and @count>1
begin
delete from netuser where UserNo=@UserNo
end
if @dep='0015' and @count>1
begin
delete from netuser where UserNo=@UserNo
end