• One other change I made so that this works for all databses:

    INSERT INTO #TMP_DB

    SELECT DBName = '[' + LTRIM(RTRIM(name)) +']'

    FROM master.dbo.sysdatabases

    WHERE category IN ('0', '1','16')

    AND DATABASEPROPERTYEX(name, 'status') = 'ONLINE'

    ORDER BY name

    I added the brackets since I have databases with '.' (periods) in them.