为什么我的存储过程在没有声明的情况下回滚,声明它是这样做的

时间:2012-01-18 18:30:44

标签: sql sql-server-2005 stored-procedures asp.net-membership

我正在尝试将用户从系统中存在的自定义用户表导入到.net成员资格提供程序表中。

我构建了一个小应用程序,删除.net成员资格表中除我自己的帐户之外的所有现有用户。我这样编写它,以便每次运行应用程序时它都会清除我添加的任何内容并重新输入所有用户的数据。这个存储过程已经过去了,但自从切换到这个新的SQL Server 2005 Enterprise实例后,删除事务一直在回滚

这是我用来在将用户写回.net成员资格表之前清除所有数据的存储过程。

ALTER PROCEDURE [dbo].[usp_ClearAllUsers]

AS
begin

DECLARE @UserID  uniqueidentifier

    set @UserID = (SELECT     UserId
FROM         dbo.aspnet_Users
where (UserName = 'gianluca.sirianni'))


begin
Delete from  dbo.aspnet_Membership WHERE (dbo.aspnet_Membership.UserID !=  @UserID) 
end

begin
Delete from  dbo.aspnet_UsersInRoles   WHERE (dbo.aspnet_UsersInRoles.UserID !=  @UserID) 
end

begin
Delete from  dbo.aspnet_Profile   WHERE ( dbo.aspnet_Profile.UserID !=  @UserID) 
end

begin
Delete from  dbo.aspnet_Users  WHERE (dbo.aspnet_Users.UserID !=  @UserID) 
end

end

有人可以建议为什么我的数据会回滚吗?我对SQL语句非常流利,但对服务器本身的事务处理却不熟悉。

更新

我将上述程序改为以下程序。这是对的吗?

ALTER PROCEDURE [dbo].[usp_ClearAllUsers]

AS
BEGIN 

DECLARE @UserID  uniqueidentifier

    SET @UserID = (SELECT     UserId
FROM         dbo.aspnet_Users
WHERE (UserName = 'gianluca.sirianni'))


BEGIN TRAN
DELETE FROM  dbo.aspnet_Membership WHERE (dbo.aspnet_Membership.UserID !=  @UserID) 
COMMIT TRAN

BEGIN TRAN
DELETE FROM  dbo.aspnet_UsersInRoles   WHERE (dbo.aspnet_UsersInRoles.UserID !=  @UserID) 
COMMIT TRAN

BEGIN TRAN
DELETE FROM dbo.aspnet_Profile   WHERE ( dbo.aspnet_Profile.UserID !=  @UserID) 
COMMIT TRAN

BEGIN TRAN
DELETE FROM  dbo.aspnet_Users  WHERE (dbo.aspnet_Users.UserID !=  @UserID) 
COMMIT TRAN

END

1 个答案:

答案 0 :(得分:1)

这是因为隐式事务已启用 AND 您未明确提交更改。 您可以通过执行SET IMPLICIT_TRANSACTIONS OFF 来设置隐式事务,也可以在COMMIT TRANSACTION语句后添加DELETE