Если у вас была проблема с невозможностью удаления пользователя из БД, то вам может помочь следующий способ. Предположим что вы получили ошибку от SQL Server:
Msg 15136, Level 16, State 1, Line 2
The database principal is set as the execution context of one or more procedures, functions, or event notifications and cannot be dropped.
Предлагаю начать изучать проблему с sys.sql_modules. По колонке execute_as_principal_id мы сможем понять какие объекты вызываются от контекста другого пользователя
Далее нам необходимо понять какие пользователи в этом задействованы. Первым делом необходимо рассмотреть какие объекты имеют свойство execute_as_principal_id
select object_name(object_id) 'view name' from sys.system_views where object_definition (object_id) like '%execute_as_principal_id%'
На основе полученных данных можно обратиться ко всем перечисленным объектам и получить необходимые нам имена пользователей:
select user_name(execute_as_principal_id) 'execute as user', * from sys.system_sql_modules where execute_as_principal_id is not null select user_name(execute_as_principal_id) 'execute as user', * from sys.service_queues where execute_as_principal_id is not null select user_name(execute_as_principal_id) 'execute as user', * from sys.assembly_modules where execute_as_principal_id is not null select user_name(execute_as_principal_id) 'execute as user', * from sys.sql_modules where execute_as_principal_id is not null select user_name(execute_as_principal_id) 'execute as user', * from sys.server_assembly_modules where execute_as_principal_id is not null select user_name(execute_as_principal_id) 'execute as user', * from sys.server_sql_modules where execute_as_principal_id is not null