USE [db_NLMRPII] GO /****** 对象: StoredProcedure [dbo].[p_RolePermission] 脚本日期: 06/22/2011 07:48:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[p_RolePermission] -- ======================================== -- 角色的权限 -- ======================================== -- 参数: -- 返回值: -- 注:5次失败尝试。(锁定失败、死锁) -- Livon 2011-06-22 -- ======================================== ( @strAction varChar(1000) -- 操作类型: 取值: Insert 、Update 、Delete ,@intRolePermissionIds varChar(1000) -- 主键(组) ,@intRoleId varChar(1000) -- ,@intPermissionId varChar(1000) -- ,@strNote varChar(1000) -- 备注 ) AS -- 定义变量 ------------------------------------------------- DECLARE @Err INT -- 错误编号 ,@ErrCounter INT -- 错误次数 ,@ErrCounterMax INT -- 最大错误次数 ,@code varChar(1000) -- 返回代码 ,@message varChar(1000) -- 返回信息 BEGIN BEGIN TRANSACTION TRANS SET @ErrCounter = 0 SET @ErrCounterMax = 5 LockTimeOutRetry: -- 锁定超时入口 BEGIN TRY -- (1)Insert ------------------------------------------------- IF @strAction = 'Insert' INSERT INTO t_RolePermission( f_intRoleId ,f_intPermissionId ,f_strNote ) VALUES( @intRoleId ,@intPermissionId ,@strNote ) -- (2)Update ------------------------------------------------- IF @strAction = 'Update' update t_RolePermission set f_intRoleId = @intRoleId , f_intPermissionId = @intPermissionId , f_dtmLastModified = getDate() , f_strNote = @strNote where f_intRolePermissionId = @intRolePermissionIds -- (3)Delete ------------------------------------------------- IF @strAction = 'Delete' begin DECLARE @delSQL VARCHAR(MAX) set @delSQL = ' Delete t_RolePermission where [f_intRolePermissionId] in ( ' + @intRolePermissionIds + ' )' exec( @delSQL ) end END TRY -- 错误检测 ------------------------------------------------- BEGIN CATCH set @Err = @@ERROR IF @Err <> 0 BEGIN ROLLBACK TRANSACTION TRANS GOTO ErrorHandler END COMMIT TRANSACTION TRANS END CATCH -- Success select 0 as code, 'Success!' as message RETURN -- 错误处理 ------------------------------------------------- ErrorHandler: IF (@err = 1222 OR @err = 1205) AND @ErrCounter >= @ErrCounterMax Begin RAISERROR('Unable to Lock Data after a few attempts.',16,1) set @code = @err SELECT @message =ERROR_MESSAGE() END IF @err = 1222 OR @err = 1205 -- Lock Timeout / Deadlock BEGIN WAITFOR DELAY '00:00:00.25' SET @ErrCounter = @ErrCounter + 1 GOTO LockTimeOutRetry END -- else unknown error set @code = @err SELECT @message = ERROR_MESSAGE() -- RAISERROR( @message ,16,1) select @code as code, @message as message END RETURN -- end 执行语句: EXEC [dbo].[p_RolePermission] @strAction = N'Set', @intRolePermissionIds = NULL, @intRoleIds = N'109', @intPermissionIds = N'2,4,5,8', @strNote = NULL