• As stated above, the parser must be able to resolve the table at compile time. So variables must be declared table variables or a name given.

    You can

    exec(' select * from ' + @tablename)

    or you need to select * from MyTable, and have the tables built in the script.