Dynamic SQL - Picking the values from the temp table

Viewed 1040

I have a temp table that has the following information.

TableName ColumnName PrimaryKeyColumnName Primarykey(A GUID)

(4 columns)

I need to include one more new column Value that should grab data based on the condition below

"for each row in the temp table"

select ColumnName from  TableName  where PrimaryKeyColumnName  = Primarykey  

To be even more precise , the query must retrieve the values from the table directly.

I am sure this can be achieved using cursor. But it might impact my query performance. Could this be achieved using sp_sqlexecute? I tried converting the query as a varchar.

Like this

set @sql = 'select '+ #final.[primary field] +'from ' + #final.tablename +
                'where '+ #final.PrimaryKeyColumnName  + '='+ #final.Primarykey  

exec sp_sqlexecute  @sql
3 Answers
Related