Quick bug fix.
The line "EXEC ('USE '+@DB_NAME+';"
Should be "EXEC ('USE ['+@DB_NAME+'];"
otherwise it fails if you have a database name with spaces in.
Not that any of us would do this, but third parties who don't know any better do/did.
Otherwise a very useful script.