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
I am not sure that I understood what you are trying to do but try this:
Notice that the @sql is limited -in my sample 8000- if the temp table you have is too big you gonna have to make a separate executes,needs some changes in the code. Always you have to set an initial value for @SQL as null. I think no need for cursor, this will be solved using some SQL tricks. Hope this will help...