Use [DB_Name]
IF OBJECT_ID('Tempdb..#Tmp') IS NOT NULL DROP TABLE #Tmp
select name into #Tmp from sys.database_principals where type <> 'R' and name not in ('dbo','public','guest','sys','INFORMATION_SCHEMA')
except
select name from sys.server_principals
SELECT 'IF EXISTS (SELECT [name]...