Invalid object when selecting from temporary table
An option which I've used before is to create the temp table using some known fields that will always be the same, then alter the temp table using dynamic SQL to add in the additional columns that you only need in specific cases.  This way the temp table is created within scope, then altered via dynamic SQL.

CREATE TABLE #MyTable (ID int identity(1,1) primary key)

if @Something = 1 then
begin
     set @sql = 'alter table #MyTable add column ...'
     exec (@sql)
end
...
Your dynamic select statement goes down here somewhere.