text: ﻿CREATE PROCEDURE [dbo].aspnet_Users_DeleteUser
      @ApplicationName  nvarchar(256),
      @UserName         nvarchar(256),
      @TablesToDeleteFrom int,
      @NumTablesDeletedFrom int OUTPUT
  AS
  BEGIN
  
  	DECLARE @LoweredApplicationName  nvarchar(256)
  	SET @LoweredApplicationName = LOWER(@ApplicationName)
  
  	DECLARE @LoweredUserName nvarchar(256)
  	SET @LoweredUserName = LOWER(@UserName)
  
      DECLARE @UserId               uniqueidentifier
      SELECT  @UserId               = NULL
      SELECT  @NumTablesDeletedFrom = 0
  
      DECLARE @TranStarted   bit
      SET @TranStarted = 0
  
      IF( @@TRANCOUNT = 0 )
      BEGIN
  	    BEGIN TRANSACTION
  	    SET @TranStarted = 1
      END
      ELSE
  	SET @TranStarted = 0
  
      DECLARE @ErrorCode   int
      DECLARE @RowCount    int
  
      SET @ErrorCode = 0
      SET @RowCount  = 0
  
      SELECT  @UserId = u.UserId
  	FROM    dbo.aspnet_Users u
      INNER JOIN dbo.aspnet_Applications a ON u.ApplicationId = a.ApplicationId
      WHERE   u.LoweredUserName        = @LoweredUserName
          AND a.LoweredApplicationName = @LoweredApplicationName
  
      IF (@UserId IS NULL)
      BEGIN
          GOTO Cleanup
      END
  
      -- Delete from Membership table if (@TablesToDeleteFrom & 1) is set
      IF ((@TablesToDeleteFrom & 1) <> 0 AND
          (EXISTS (SELECT name FROM sysobjects WHERE (name = N'vw_aspnet_MembershipUsers') AND (type = 'V'))))
      BEGIN
          DELETE FROM dbo.aspnet_Membership WHERE @UserId = UserId
  
          SELECT @ErrorCode = @@ERROR,
                 @RowCount = @@ROWCOUNT
  
          IF( @ErrorCode <> 0 )
              GOTO Cleanup
  
          IF (@RowCount <> 0)
              SELECT  @NumTablesDeletedFrom = @NumTablesDeletedFrom + 1
      END
  
      -- Delete from aspnet_UsersInRoles table if (@TablesToDeleteFrom & 2) is set
      IF ((@TablesToDeleteFrom & 2) <> 0  AND
          (EXISTS (SELECT name FROM sysobjects WHERE (name = N'vw_aspnet_UsersInRoles') AND (type = 'V'))) )
      BEGIN
          DELETE FROM dbo.aspnet_UsersInRoles WHERE @UserId = UserId
  
          SELECT @ErrorCode = @@ERROR,
                  @RowCount = @@ROWCOUNT
  
          IF( @ErrorCode <> 0 )
              GOTO Cleanup
  
          IF (@RowCount <> 0)
              SELECT  @NumTablesDeletedFrom = @NumTablesDeletedFrom + 1
      END
  
      -- Delete from aspnet_Users table if (@TablesToDeleteFrom & 1,2,4 & 8) are all set
      IF ((@TablesToDeleteFrom & 1) <> 0 AND
          (@TablesToDeleteFrom & 2) <> 0 AND
          (@TablesToDeleteFrom & 4) <> 0 AND
          (@TablesToDeleteFrom & 8) <> 0 AND
          (EXISTS (SELECT UserId FROM dbo.aspnet_Users WHERE @UserId = UserId)))
      BEGIN
          DELETE FROM dbo.aspnet_Users WHERE @UserId = UserId
  
          SELECT @ErrorCode = @@ERROR,
                  @RowCount = @@ROWCOUNT
  
          IF( @ErrorCode <> 0 )
              GOTO Cleanup
  
          IF (@RowCount <> 0)
              SELECT  @NumTablesDeletedFrom = @NumTablesDeletedFrom + 1
      END
  
      IF( @TranStarted = 1 )
      BEGIN
  	    SET @TranStarted = 0
  	    COMMIT TRANSACTION
      END
  
      RETURN 0
  
  Cleanup:
      SET @NumTablesDeletedFrom = 0
  
      IF( @TranStarted = 1 )
      BEGIN
          SET @TranStarted = 0
  	    ROLLBACK TRANSACTION
      END
  
      RETURN @ErrorCode
  
  END