[SQL Server] Afficher les logins de toutes les bases de données
--
=============================================
--
Author: Zouhaier KHARROUBI
--
Create date: 31/05/2022
-- Description:
Afficher les logins de toutes les bases de données
--
=============================================
IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[#helpuserResultat]'))
BEGIN
DROP TABLE #helpuserResultat
END
CREATE TABLE #helpuserResultat
(
BaseName sysname DEFAULT DB_NAME()
,UserName sysname NULL
,RoleName sysname NULL
,LoginName sysname NULL
,DefDBName sysname NULL
,DefSchemaName sysname NULL
,UserId smallint NULL
,[SID] smallint NULL
)
execute sp_MSforeachdb '
if UPPER(''?'') like ''%_BI''
BEGIN
USE [?];
INSERT INTO #helpuserResultat (UserName
,RoleName
,LoginName
,DefDBName
,DefSchemaName
,UserId
,[SID])
EXEC (''sp_helpuser'')
END
'
SELECT 'CREATE LOGIN ' + QUOTENAME(Username) + ' FROM WINDOWS WITH
DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]' AS ScriptCreateLogin
,* FROM #helpuserResultat
WHERE UserName
LIKE '%-DEV'
SELECT DISTINCT 'CREATE LOGIN ' + QUOTENAME(Username) + ' FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]' AS ScriptCreateLogin
,UserName
--,RoleName
FROM #helpuserResultat
WHERE UserName
LIKE '%-DEV'
DROP TABLE #helpuserResultat