SQL Server – How to Drop ‘\user’ from Database

sql serversql-server-2016

Running SELECT * FROM dbName.dbo.sysusers ORDER BY name; reveals users that start with \, and this causes errors when try to add the same user without the slash.

Can't run EXEC sp_dropuser or DROP USER. This throws an error telling that user does not exist or you do not have permission.

Any ideas on how to remove the user? I believe that the \user is orphaned from a database migration from SQL Server 2000 or 2005. The current database is running on SQL Server 2016. I've tried using the GUI and T-SQL.

I've tried EXEC sp_dropuser and DROP USER using [\user], '\user',\user, QUOTENAME('\user').

I get either

Incorrect syntax

or

Cannot drop the user '\user', because it does not exist or you do not have permission.

errors.

Here is the row from the sysusers table.

uid              23
status           0
name             \User
sid              0x010500000000000512000000FD77B156780C942546170A3273170200
roles            NULL
createdate       2012-08-17 10:01:05.600
updatedate       2012-08-17 10:01:05.600
altuid           NULL
password         NULL
gid              0
environ          NULL
hasdbaccess      0
islogin          1
isntname         0
isntgroup        0
isntuser         0
issqluser        0
isaliased        0
issqlrole        0
isapprole        0

Best Answer

Try putting the name in brackets.

DROP USER [\username];

Given the problems you are having try doing this through dynamic SQL just to confirm that you are catching every possible character. You could also try this through Object Explorer.

DECLARE @sql nvarchar(1000)
SELECT @sql = N'DROP USER [' + name + N']' FROM sys.database_principals WHERE name like '\user%'
PRINT @sql
EXEC sp_executesql @sql

@solomonrutsky brought up an interesting question. Try running the following:

SELECT s.name AS Sysusers_Name, dp.name AS DP_Name
FROM sys.sysusers s
JOIN sys.database_principals dp
    ON s.sid = dp.sid
WHERE s.name = '\user'

See if you get any results, and if so if the Sysusers_name doesn't equal the DP_Name. If you get a DP_Name try dropping that one instead.