I’m trying to import users from a custom users table that existed in the system into the .net membership provider tables.
I built a small application that delete’s all pre existing users within the .net membership tables except for my own account. I wrote it this way so that each time I run the application it clears out anything I’ve added and re- enters all the user’s data. This stored proc worked int eh past but ever since switching to this new instance of SQL Server 2005 Enterprise the delete transaction keeps rolling back
Here is the stored procedure I use to clear out all my data prior to writing the users back into the .net membership tables.
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
Can some one suggest why my data may be rolling back? I’m fairly fluent in SQL statements but not on the transaction handling of the server itself.
Update:
I changed the above procedure to the following. Is this correct?
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
This happens because implicit transactions are turned on AND you don’t explicitly commit your changes.
You can either set implicit transactions off by executing
SET IMPLICIT_TRANSACTIONS OFF
or putCOMMIT TRANSACTIONafter yourDELETEstatements.