declare @cmd	varchar(8000), 
        @name varchar (255)

USE master

SET NOCOUNT ON

declare c_databases cursor local forward_only static for
  SELECT name
    FROM sys.databases
  WHERE name not in ('master','msdb','tempDB','model','SSISDB')  
  AND [is_read_only] = 0
  AND [state_desc] = 'ONLINE'
  ORDER BY name
;
open c_databases
;
FETCH NEXT FROM c_databases
  into @name
;
while @@fetch_status = 0
begin
    SET @cmd = 'declare @name varchar(50)
    
    declare c_users cursor local forward_only static for
     SELECT dp.name 
       FROM '+@name+'.sys.database_principals AS dp  
  LEFT JOIN '+@name+'.sys.server_principals AS sp  
         ON dp.SID = sp.SID  
      WHERE sp.SID IS NULL  
        AND dp.type_desc = ''SQL_USER''
        AND dp.principal_id > 4;
  
    open c_users

    FETCH NEXT FROM c_users
    into @name
  
    while @@fetch_status = 0
    begin
      
     EXEC (''USE ['+@name+']; DROP USER ''+@name+'''')
	      
      FETCH NEXT FROM c_users
      into @name
  
    end
    close c_users
    deallocate c_users
  ' /* #fla01 */

   exec (@cmd)
   
   FETCH NEXT FROM c_databases
    into @name
  ;
end
;
close c_databases
;
deallocate c_databases
