Error while dynamically build query and execute it


When we dynamically build the query and try to execute query a external tables it gives below error.

(SELECT @SelectClause  from @TableName;): Invalid object name 'Companies'.


1. Are you sure you need to do it that way? It is not very good for performance...

2. Have you marked the input parameters as Expand Inline?

Agree with Nuno, I don't think this is a good way. But try to set expand inline to 'yes'.

Is the entity "Companies" referenced by the espace?

Why are you build the query dinamicaly? Do you realy need it? That's not good for performance because database engine will never be able to build a query execution plan


Check the below post where there is an accepted solution.

As others have stated, go the dynamic sql way, only if this cant be achieved via aggregates. 

It would be nice if you can share some context, so the forum can help better.