I am pretty sure that this statement is not accomplishing what you intended.
exec('USE ' + @YourDatabaseName)
When Exec is executed the database context is changed, but only for the statement(s) in the parenthsis. When Exex completes the database context returns to its previous setting.
You can see that behavior with the following test script.
Declare @myDatabase varChar(255)
Set @myDatabase = 'Northwind'
Exec ('Use ' + @myDatabase + '; Select db_Name(); Select [name] from sys.objects where type = ''U'' ')
Select [name] from sys.objects where type = 'U'
You will see that the database context returns to AdventureWorks after the conclusion of the Exec script.
I am still looking for a reasonable methog to dynamically change the database context so if you find one please post it