2 replaced http://dba.stackexchange.com/ with https://dba.stackexchange.com/
source | link

If you just have one login, then manually quotingmanually quoting it with [ ] will work. If you have many of them then you have to build a dynamic sql like below to progrmatically get the drop login [login_to_drop] from sys.server_principals using QUOTENAME() tsql

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql+= N'DROP LOGIN ' + QUOTENAME(name) + ';' 
FROM sys.server_principals 
WHERE name <> N'sa'                 -- do not drop SA
AND name NOT LIKE N'##%'            -- special logins 
AND name NOT LIKE N'NT [AS]%'       -- special logins NT related
AND [type] IN ('S', 'U', 'G','R')   -- S = SQL login | U = Windows login | G = Windows group | R = Server role
AND principal_id > 256
--AND name in ()                    -- Filter to drop specific logins
AND name <> SUSER_SNAME();          -- This will avoid yourself for being dropped !

PRINT @sql;
-- once you verify that below logins will be dropped, 
-- uncomment below line 
-- EXEC master.sys.sp_executesql @sql;

Lesson learned, dont use special characters .. else be prepared to deal with them with some pain :-)

If you just have one login, then manually quoting it with [ ] will work. If you have many of them then you have to build a dynamic sql like below to progrmatically get the drop login [login_to_drop] from sys.server_principals using QUOTENAME() tsql

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql+= N'DROP LOGIN ' + QUOTENAME(name) + ';' 
FROM sys.server_principals 
WHERE name <> N'sa'                 -- do not drop SA
AND name NOT LIKE N'##%'            -- special logins 
AND name NOT LIKE N'NT [AS]%'       -- special logins NT related
AND [type] IN ('S', 'U', 'G','R')   -- S = SQL login | U = Windows login | G = Windows group | R = Server role
AND principal_id > 256
--AND name in ()                    -- Filter to drop specific logins
AND name <> SUSER_SNAME();          -- This will avoid yourself for being dropped !

PRINT @sql;
-- once you verify that below logins will be dropped, 
-- uncomment below line 
-- EXEC master.sys.sp_executesql @sql;

Lesson learned, dont use special characters .. else be prepared to deal with them with some pain :-)

If you just have one login, then manually quoting it with [ ] will work. If you have many of them then you have to build a dynamic sql like below to progrmatically get the drop login [login_to_drop] from sys.server_principals using QUOTENAME() tsql

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql+= N'DROP LOGIN ' + QUOTENAME(name) + ';' 
FROM sys.server_principals 
WHERE name <> N'sa'                 -- do not drop SA
AND name NOT LIKE N'##%'            -- special logins 
AND name NOT LIKE N'NT [AS]%'       -- special logins NT related
AND [type] IN ('S', 'U', 'G','R')   -- S = SQL login | U = Windows login | G = Windows group | R = Server role
AND principal_id > 256
--AND name in ()                    -- Filter to drop specific logins
AND name <> SUSER_SNAME();          -- This will avoid yourself for being dropped !

PRINT @sql;
-- once you verify that below logins will be dropped, 
-- uncomment below line 
-- EXEC master.sys.sp_executesql @sql;

Lesson learned, dont use special characters .. else be prepared to deal with them with some pain :-)

1
source | link

If you just have one login, then manually quoting it with [ ] will work. If you have many of them then you have to build a dynamic sql like below to progrmatically get the drop login [login_to_drop] from sys.server_principals using QUOTENAME() tsql

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql+= N'DROP LOGIN ' + QUOTENAME(name) + ';' 
FROM sys.server_principals 
WHERE name <> N'sa'                 -- do not drop SA
AND name NOT LIKE N'##%'            -- special logins 
AND name NOT LIKE N'NT [AS]%'       -- special logins NT related
AND [type] IN ('S', 'U', 'G','R')   -- S = SQL login | U = Windows login | G = Windows group | R = Server role
AND principal_id > 256
--AND name in ()                    -- Filter to drop specific logins
AND name <> SUSER_SNAME();          -- This will avoid yourself for being dropped !

PRINT @sql;
-- once you verify that below logins will be dropped, 
-- uncomment below line 
-- EXEC master.sys.sp_executesql @sql;

Lesson learned, dont use special characters .. else be prepared to deal with them with some pain :-)