-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLTriggerssql.sql
52 lines (46 loc) · 1.11 KB
/
SQLTriggerssql.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
CREATE TRIGGER SetRoleLeaderAfterUpdateOrInsert ON dbo.Users_Teams
AFTER INSERT, UPDATE
AS
DECLARE @LeaderId NVARCHAR(450);
DECLARE @IsLeader BIT;
DECLARE @IsAlreadyLeader INT;
BEGIN
SELECT @LeaderId = i.UserId, @IsLeader = i.IsLeader
FROM inserted i;
SELECT @IsAlreadyLeader = COUNT(*)
FROM dbo.AspNetUserRoles
JOIN inserted as i
ON dbo.AspNetUserRoles.UserId = i.UserId
AND dbo.AspNetUserRoles.RoleId = 2
IF @IsLeader = 1 AND @IsAlreadyLeader <= 0
INSERT INTO dbo.AspNetUserRoles VALUES(@LeaderId, 2);
END
GO
CREATE TRIGGER SetRoleUserAfterInsert ON dbo.AspNetUsers
AFTER INSERT
AS
DECLARE @UserId NVARCHAR(450);
BEGIN
SELECT @UserId = i.Id FROM inserted i;
INSERT INTO dbo.AspNetUserRoles VALUES(@UserId, 1);
END
GO
CREATE TRIGGER DeleteLeaderRole
ON dbo.Users_Teams
AFTER DELETE
AS
DECLARE @HowManyLeader INT;
DECLARE @UserId NVARCHAR(450);
BEGIN
SELECT @UserId = UserId
FROM deleted;
SELECT @HowManyLeader = COUNT(UserId)
FROM dbo.Users_Teams
WHERE UserID = @UserId
AND IsLeader = 1;
IF @HowManyLeader = 0
DELETE FROM AspNetUserRoles
WHERE UserId = @UserId
AND RoleId = 2;
END
GO