ALTER PROCEDURE dbo.aspnet_Profile_DeleteProfiles
@ApplicationName NVARCHAR(256),
@UserNames NVARCHAR(4000)
AS
BEGIN
DECLARE @UserName NVARCHAR(256)
DECLARE @CurrentPos INT
DECLARE @NextPos INT
DECLARE @NumDeleted INT
DECLARE @DeletedUser INT
DECLARE @TranStarted BIT
DECLARE @ErrorCode INT
![](/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 0
SET @CurrentPos = 1
SET @NumDeleted = 0
SET @TranStarted = 0
-----------声明变量并为变量附初值-----------------
![](/Images/OutliningIndicators/None.gif)
IF( @@TRANCOUNT = 0 )
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
---------------如果当前活动事务为0,开始事务并设置事务参数为1---------------------
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
WHILE (@CurrentPos <= LEN(@UserNames)) --如果@CurrentPos不大于输入参数的长度
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*LEN返回给定字符串表达式的字符(而不是字节)个数,其中不包含尾随空格。*/
BEGIN
SELECT @NextPos = CHARINDEX(N',', @UserNames, @CurrentPos)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*CHARINDEX 返回字符串中指定表达式的起始位置
返回@UserNames中从@CurrentPos开始 N',' 的起始位置*/
IF (@NextPos = 0 OR @NextPos IS NULL)
SELECT @NextPos = LEN(@UserNames) + 1
![](/Images/OutliningIndicators/None.gif)
SELECT @UserName = SUBSTRING(@UserNames, @CurrentPos, @NextPos - @CurrentPos)
SELECT @CurrentPos = @NextPos+1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*SUBSTRING 返回字符、binary、text 或 image 表达式的一部分。
上面即返回@UserNames中开始位置为@CurrentPos,长度为@NextPos - @CurrentPos的部分*/
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
----------------------上面这部分有点不明白为什么要这样-----------
IF (LEN(@UserName) > 0)
BEGIN
SELECT @DeletedUser = 0
-----------------调用存储过程aspnet_Users_DeleteUser(最好看下这个存储过程先),输出参数为@DeletedUser----------
EXEC dbo.aspnet_Users_DeleteUser @ApplicationName, @UserName, 4, @DeletedUser OUTPUT
IF( @@ERROR <> 0 ) --如果存储过程执行有错误,则跳转到回滚
BEGIN
SET @ErrorCode = -1
GOTO Cleanup
END
IF (@DeletedUser <> 0) --如果存储过程的输出参数不为0,即成功了,则@NumDeleted递增1
SELECT @NumDeleted = @NumDeleted + 1
END
END
SELECT @NumDeleted ---查询 NumDeleted
IF (@TranStarted = 1) ---事务运行到此
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION ---结束事务
END
SET @TranStarted = 0
![](/Images/OutliningIndicators/None.gif)
RETURN 0
![](/Images/OutliningIndicators/None.gif)
Cleanup:
IF (@TranStarted = 1 )
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END
posted on
2006-08-31 10:09
MainIsUsed
阅读(
261)
评论()
编辑
收藏
举报