May be because Dynamic SQL is executed/Parsed last by the query Parser and the variable declared are within the statement which is limited to the "exec" and is considered a seperate stored procedure outside of main query
The EXEC statement executes within its own scope, outside of the procedure that calls it. Therefore, EXEC cannot share variables with its calling procedure. As soon as EXEC completes, the variables go out of scope and "poof", they disappear. Temporary tables, however, are connection specific and can be referenced within EXEC statements.