How can I drop user from a database without dropping it's logging?
The script should check if the user exists in database, if does then drop the user.
The DROP USER statement is used to remove a user from the SQL Server database.
Once you have identified orphan users it is extremely simple to remove them. You remove them by using the sp_revokeuser SP. Here is an example that removes the database users 'USERX', from the current database in use.
Is this what you are trying to do??
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'username') DROP USER [username]
If you are using SQL Server Management Studio you can browse to the user and right-click selecting delete.
The accepted answer is working good enough. Additionally that is good to know SQL Server added IF EXIST to some DROP commands from version 2016 (13.x) including 'DROP USER' command.
IF EXISTS
Applies to: SQL Server ( SQL Server 2016 (13.x) through current version, SQL Database).
Conditionally drops the user only if it already exists.
So you could just delete user as below:
-- Syntax for SQL Server and Azure SQL Database DROP USER IF EXISTS user_name
See the full description in this link: DROP USER (Transact-SQL)
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With