In order to do this everything which needs to be done within the other database needs to be done in the dynamic SQL. Dynamic SQL is done out of bounds of the parent code. Using this method it would be much better to simply access the objects without the USE command.
<pre>declare @db varchar(10)
set @db = ‘db1’
declare @cmd varchar(8000)
set @cmd = ‘select * from ‘ + @db