从 SQL Server 数据库中删除用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:59:59  来源:igfitidea点击:

Drop User from SQL Server Database?

sqlsql-serveruser-management

提问by sanjeev40084

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.

该脚本应检查用户是否存在于数据库中,如果存在则删除该用户。

回答by doug_w

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.

如果您使用的是 SQL Server Management Studio,您可以浏览到用户并右键单击选择删除。

回答by QMaster

The accepted answer is working good enough. Additionally that is good to know SQL Server added IF EXISTto some DROP commands from version 2016 (13.x) including 'DROP USER' command.

接受的答案已经足够好了。此外,很高兴知道 SQL Server 将IF EXIST添加到版本 2016 (13.x) 的某些 DROP 命令,包括“ DROP USER”命令。

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.

如果存在

适用于:SQL Server(SQL Server 2016 (13.x) 到当前版本,SQL 数据库)。

仅当用户已经存在时,才会有条件地删除该用户。

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)

请参阅此链接中的完整说明:DROP USER (Transact-SQL)

Hope this help.

希望这有帮助。