[RESOLVED] MS SQL - Dynamic SQL
Hi,
Situation:
A table of alterations which holds the old and new values (as Varchar).
A request from the client to not show the ID and show instead the interpreted data (ie name of a customer instead of their id).
I thought of just joining the table to the other table via id but had to utilise a convert function on the join to be able to run when a value in the log isnt a number.
I thought of using dynamic sql inside a stored proc to get a table of table names and fields and which field(s) to return. Pull back the log data into a temporary variable/table. Then loop the results, mount the Sql and get the actual data to show then update the temporary table before returning to the app to show in a grid.
I managed to get it to mount an SQL that looks correct, but MS Sql is complaining it cannot find the temporary table variable I defined.
The Questions are:
Is this possible?
When using the EXECUTE sp_executesql @newsql, does this create a separate instance type thing where it cannot see the variable declared already? Or so I need to pass the execution a pointer to the variable, somehow?
Any other ways I could try?
Re: [RESOLVED] MS SQL - Dynamic SQL
Quote:
The Field could be any field in that table which has been recorded, rather than just the pk.
Hmmm, I think you're stuck with the loop as well then. I can't see a way of feeding the Field name into the query without it... which sucks.