目前我有三个存储过程:
userId
和roleId
userId
和roleId
isShopper
userId
是唯一的,但可以有多个角色。角色是"客户","购物者"和" admin"。当我插入" shopper"的角色时列isShopper
的位值为1,这就是我想要的。
但是当我删除" shopper"的角色时来自用户,比特值保持为1.我很好奇如何在取消购物者角色时将购物者的比特值更改回0。
插入程序:
ALTER proc [dbo].[Asp_Net_User_Role_Insert]
@UserId nvarchar (128),
@RoleId nvarchar (128)
AS
BEGIN
INSERT INTO [dbo].[AspNetUserRoles] ([UserId], [RoleId])
SELECT
@UserId, @RoleId
WHERE
NOT EXISTS(SELECT UserId, RoleId
FROM AspNetUserRoles
WHERE RoleId = @RoleId
AND UserId = @UserId)
EXECUTE dbo.ProfileAccountInfos_Shopper_Update
@UserId, @RoleId
END
删除程序:
ALTER proc [dbo].[Asp_Net_User_Role_Delete]
@UserId nvarchar (128),
@RoleId nvarchar (128)
AS
BEGIN
DELETE FROM [dbo].[AspNetUserRoles]
WHERE RoleId = @RoleId
AND UserId = @UserId
EXECUTE dbo.ProfileAccountInfos_Shopper_Update
@UserId, @RoleId
END
更新程序(在插入和删除的末尾标记)
ALTER proc dbo.ProfileAccountInfos_Shopper_Update
@IsShopper bit,
@UserId nvarchar (128),
@RoleId nvarchar(128)
AS
BEGIN
IF (@RoleId = '91e67659-0dcb-4171-bc76-78d18b1d1336')
AND @IsShopper = 0
BEGIN
UPDATE [dbo].[ProfileAccountInfos]
SET [DateModified] = getutcdate(),
[IsShopper] = 1
WHERE UserId = @UserId
END
END
答案 0 :(得分:0)
在程序" ProfileAccountInfos_Shopper_Update"您有参数@IsShooper但是当您从" Asp_Net_User_Role_Delete"执行时,您没有传递该参数的值。或" Asp_Net_User_Role_Insert"。
如果您的RoleId值始终相同,那么您可以在Asp_Net_User_Role_Insert
和Asp_Net_User_Role_Delete
过程中确定@IsShopper的值并将其传递给ProfileAccountInfos_Shopper_Update
程序。您在ProfileAccountInfos_Shopper_Update
过程中不需要@RoleId参数。
所以ProfileAccountInfos_Shopper_Update
将简化如下。
ALTER proc dbo.ProfileAccountInfos_Shopper_Update
,@UserId nvarchar (128)
,@IsShopper BIT
AS
BEGIN
UPDATE [dbo].[ProfileAccountInfos] SET [DateModified] = getutcdate(),[IsShopper] = @IsShopper WHERE UserId = @UserI
END
程序Asp_Net_User_Role_Insert
将更改如下。
ALTER proc [dbo].[Asp_Net_User_Role_Insert]
@UserId nvarchar (128),
@RoleId nvarchar (128)
AS
BEGIN
DECLARE @IsShoper BIT
SET @IsShopper = 0
INSERT INTO [dbo].[AspNetUserRoles] ([UserId], [RoleId])
SELECT
@UserId, @RoleId
WHERE
NOT EXISTS(SELECT UserId, RoleId
FROM AspNetUserRoles
WHERE RoleId = @RoleId
AND UserId = @UserId)
IF (@RoleId = '91e67659-0dcb-4171-bc76-78d18b1d1336') -- If role being inserted is Shopper then mark user as a Shopper.
BEGIN
SET @IsShopper = 1
END
EXECUTE dbo.ProfileAccountInfos_Shopper_Update @UserId, @IsShopper
END
程序Asp_Net_User_Role_Insert
将更改如下。
ALTER proc [dbo].[Asp_Net_User_Role_Delete]
@UserId nvarchar (128),
@RoleId nvarchar (128)
AS
BEGIN
DECLARE @IsShoper BIT
SET @IsShopper = 1
DELETE FROM [dbo].[AspNetUserRoles] WHERE RoleId = @RoleId AND UserId = @UserId
IF (@RoleId = '91e67659-0dcb-4171-bc76-78d18b1d1336') -- If role being deleted is Shopper then mark user as non-shopper.
BEGIN
SET @IsShopper = 0
END
EXECUTE dbo.ProfileAccountInfos_Shopper_Update @UserId, @IsShopper
END
您还可以避免使用ProfileAccountInfos_Shopper_Update
存储过程。您可以将更新语句放在Asp_Net_User_Role_Insert
和Asp_Net_User_Role_Delete