【发布时间】:2014-05-01 17:40:47
【问题描述】:
也许我遗漏了一些东西,但即使下面的 RAISERRORs 的严重性为 16(根据文档),事务仍然提交,就好像 XACT_ABORT ON 没有效果一样。
CREATE PROCEDURE [ExploringGroups].[RemoveMember]
@groupId uniqueidentifier,
@adminUsername nvarchar(50),
@targetUsername nvarchar(50)
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
DECLARE
@adminUserId uniqueidentifier = dbo.fn_userId(@adminUsername),
@targetUserId uniqueidentifier = dbo.fn_userId(@targetUsername)
IF @targetUserId IS NULL OR ExploringGroups.IsMember(@groupId, @targetUserId) = 0
RAISERROR('Target user was not located', 16, 1)
IF ExploringGroups.IsInRole(@groupId, @adminUserId, 'adm') = 0
RAISERROR('Specified user is not an administrator of this group', 16, 2)
IF @adminUserId = @targetUserId
RAISERROR('You cannot remove yourself', 16, 3)
-- statements below still execute and commit even though there was an error raised above
DELETE FROM ExploringGroups.MemberRole WHERE GroupId = @groupId AND UserId = @targetUserId
DELETE FROM ExploringGroups.Membership WHERE GroupId = @groupId AND UserId = @targetUserId
COMMIT
RETURN 0
打电话
exec exploringgroups.removemember '356048C5-BAB3-45C9-BE3C-A7227225DFDD', 'Crypton', 'Crypton'
生产
消息 50000,级别 16,状态 2,过程 RemoveMember,第 20 行
指定用户不是该组的管理员
消息 50000,级别 16,状态 3,过程 RemoveMember,第 24 行
你不能删除自己
我认为XACT_ABORT 应该回滚整个事务,如果它设置为ON?
【问题讨论】:
标签: sql-server tsql sql-server-2008-r2 xact-abort