If you need to copy a login without knowing the password

20 Aug 2019 0 minutes to read Contributors

SELECT 'IF(SUSER_ID('+QUOTENAME(SL.name,'''')+') IS NULL)
BEGIN
CREATE LOGIN '+QUOTENAME(SL.name)+' WITH PASSWORD = '+CONVERT(NVARCHAR(MAX),SL.password_hash,1)+' HASHED, 
SID = '+CONVERT(NVARCHAR(MAX),SL.sid,1)+' END'  FROM sys.sql_logins AS SL WHERE name not like '##%##' and name <> 'sa' order by name

In this article