EXECUTE sp_executesql
Assuming that the temporary tables that you are referencing exists, then your problem is session scope. Temporary tables marked with a single # are visible to the session that they are created in, they are also known as local temporary tables.

The problem that you have is that when you execute a piece of dynamic sql it runs in a new session - hence that session cannot see the temporary tables that you have created.

To fix the problem you could do one of 3 things:


Create the temporary table within your dynamic sql
Use a global temporary table using ## as these are visible across all sessions
Re-design your code to insert into a table variable and return that as an output parameter to sp_executesql


I hope this helps you.