ALTER PROCEDURE dbo.aspnet_UsersInRoles_RemoveUsersFromRoles
@ApplicationName NVARCHAR(256),
@UserNames NVARCHAR(4000),
@RoleNames NVARCHAR(4000)
AS
BEGIN
DECLARE @ApplicationId UNIQUEIDENTIFIER
SELECT @ApplicationId = NULL
SELECT @ApplicationId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
IF (@ApplicationId IS NULL)
RETURN(2)
/*返回@ApplicationId,不存在则返回2*/
DECLARE @TranStarted BIT
DECLARE @ErrorCode INT
SET @ErrorCode = 0
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) --如果当前活动事务为0,开始事务并设置事务参数为1
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
DECLARE @RoleId UNIQUEIDENTIFIER
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @UserName NVARCHAR(256)
DECLARE @RoleName NVARCHAR(256)
DECLARE @CurrentPosU INT
DECLARE @NextPosU INT
DECLARE @CurrentPosR INT
DECLARE @NextPosR INT
SELECT @CurrentPosU = 1
-----------------------------------------------------------------------------------
---------------又是那个-------------------------------------
WHILE(@CurrentPosU <= LEN(@UserNames))
BEGIN
SELECT @NextPosU = CHARINDEX(N',', @UserNames, @CurrentPosU)
IF (@NextPosU = 0 OR @NextPosU IS NULL)
SELECT @NextPosU = LEN(@UserNames)+1
SELECT @UserName = SUBSTRING(@UserNames, @CurrentPosU, @NextPosU - @CurrentPosU)
SELECT @CurrentPosU = @NextPosU+1
SELECT @CurrentPosR = 1
WHILE(@CurrentPosR <= LEN(@RoleNames))
BEGIN
SELECT @NextPosR = CHARINDEX(N',', @RoleNames, @CurrentPosR)
IF (@NextPosR = 0 OR @NextPosR IS NULL)
SELECT @NextPosR = LEN(@RoleNames)+1
SELECT @RoleName = SUBSTRING(@RoleNames, @CurrentPosR, @NextPosR - @CurrentPosR)
SELECT @CurrentPosR = @NextPosR+1
------------------------------------对用户名和角色名的操作----------------
SELECT @RoleId = NULL
SELECT @RoleId = RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId
/*查询角色ID*/
IF (@RoleId IS NULL) --如果不存在则跳转回滚
BEGIN
SELECT N'', @RoleName
SET @ErrorCode = 2
GOTO Cleanup
END
SELECT @UserId = NULL
SELECT @UserId = UserId FROM dbo.aspnet_Users WHERE LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId
/*查询用户ID*/
IF (@UserId IS NULL) ---如果不存在则跳转回滚
BEGIN
SELECT @UserName, N''
SET @ErrorCode = 1
GOTO Cleanup
END
IF (NOT(EXISTS(SELECT * FROM dbo.aspnet_UsersInRoles WHERE @UserId = UserId AND @RoleId = RoleId)))
/*如果此用户不存在于此角色中,跳转回滚*/
BEGIN
SELECT @UserName, @RoleName
SET @ErrorCode = 3
GOTO Cleanup
END
DELETE FROM dbo.aspnet_UsersInRoles WHERE (UserId = @UserId AND RoleId = @RoleId)
/*如果存在于此角色中,则删除此记录*/
END
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION --结束事务
END
RETURN(0)
Cleanup:
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END
@ApplicationName NVARCHAR(256),
@UserNames NVARCHAR(4000),
@RoleNames NVARCHAR(4000)
AS
BEGIN
DECLARE @ApplicationId UNIQUEIDENTIFIER
SELECT @ApplicationId = NULL
SELECT @ApplicationId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
IF (@ApplicationId IS NULL)
RETURN(2)
/*返回@ApplicationId,不存在则返回2*/
DECLARE @TranStarted BIT
DECLARE @ErrorCode INT
SET @ErrorCode = 0
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) --如果当前活动事务为0,开始事务并设置事务参数为1
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
DECLARE @RoleId UNIQUEIDENTIFIER
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @UserName NVARCHAR(256)
DECLARE @RoleName NVARCHAR(256)
DECLARE @CurrentPosU INT
DECLARE @NextPosU INT
DECLARE @CurrentPosR INT
DECLARE @NextPosR INT
SELECT @CurrentPosU = 1
-----------------------------------------------------------------------------------
---------------又是那个-------------------------------------
WHILE(@CurrentPosU <= LEN(@UserNames))
BEGIN
SELECT @NextPosU = CHARINDEX(N',', @UserNames, @CurrentPosU)
IF (@NextPosU = 0 OR @NextPosU IS NULL)
SELECT @NextPosU = LEN(@UserNames)+1
SELECT @UserName = SUBSTRING(@UserNames, @CurrentPosU, @NextPosU - @CurrentPosU)
SELECT @CurrentPosU = @NextPosU+1
SELECT @CurrentPosR = 1
WHILE(@CurrentPosR <= LEN(@RoleNames))
BEGIN
SELECT @NextPosR = CHARINDEX(N',', @RoleNames, @CurrentPosR)
IF (@NextPosR = 0 OR @NextPosR IS NULL)
SELECT @NextPosR = LEN(@RoleNames)+1
SELECT @RoleName = SUBSTRING(@RoleNames, @CurrentPosR, @NextPosR - @CurrentPosR)
SELECT @CurrentPosR = @NextPosR+1
------------------------------------对用户名和角色名的操作----------------
SELECT @RoleId = NULL
SELECT @RoleId = RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId
/*查询角色ID*/
IF (@RoleId IS NULL) --如果不存在则跳转回滚
BEGIN
SELECT N'', @RoleName
SET @ErrorCode = 2
GOTO Cleanup
END
SELECT @UserId = NULL
SELECT @UserId = UserId FROM dbo.aspnet_Users WHERE LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId
/*查询用户ID*/
IF (@UserId IS NULL) ---如果不存在则跳转回滚
BEGIN
SELECT @UserName, N''
SET @ErrorCode = 1
GOTO Cleanup
END
IF (NOT(EXISTS(SELECT * FROM dbo.aspnet_UsersInRoles WHERE @UserId = UserId AND @RoleId = RoleId)))
/*如果此用户不存在于此角色中,跳转回滚*/
BEGIN
SELECT @UserName, @RoleName
SET @ErrorCode = 3
GOTO Cleanup
END
DELETE FROM dbo.aspnet_UsersInRoles WHERE (UserId = @UserId AND RoleId = @RoleId)
/*如果存在于此角色中,则删除此记录*/
END
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION --结束事务
END
RETURN(0)
Cleanup:
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END