Thanks for the tip on the database name. What I don't understand is on the line Set @command = '. This line has a single quote after the @command and the other quote is way after the END line.
Do you mean the whole block from Use [database name] to End is meant to be included in the single quotes?