I am running a dynamic SQL which is giving me an error about "multi-part identifiers". As I have identically-named columns in different tables, I am prefixing the column name with a table alias, which seems to be the problem.
The reported errors are:
The multi-part identifier
e.Categorydescription
could not be bound.
The multi-part identifierl.FullAddress
could not be bound.
The column Categorydescription
exists in tables Events
and Categories
, and column FullAddress
exists in tables Events
and Location
, hence I am using e.Categorydescription
and l.FullAddress
. I cannot drop the inner joins as I need other columns from the tables Location
and Categories
.
I misjudged the error. The actual error is further down (below) in the code where I read from ##Results global table:
SET @s_query = 'SELECT ' + @ColNames + ' FROM ##Results
WHERE ##RowNum BETWEEN('+CONVERT(varchar(20),@PageIndex)+'-1) * '+
CONVERT(varchar(20),@PageSize)+' + 1
AND((('+CONVERT(varchar(20),@PageIndex)+' -1) * '+
CONVERT(varchar(20),@PageSize)+' + 1) + '+CONVERT(varchar(20),@PageSize)+') - 1
ORDER BY ##RowNum';
EXEC (@s_query); -- the error is from here
--because #Results# has "Categorydescription" instead of "e.Categorydescription"