I have a situation where I need to generate a table from a stored procedure for several different clients. These tables all have the same core values but sometimes they have additional columns.
I’d rather not be copying and pasting those table generating bits of SQL every time so my current setup is that I have a stored procedure that returns the core columns and this gets loaded into ##TempTableClientName (necessary because it is an external app accessing these and each command sent to the server seems to be seen as a different connection so a standard session temp table doesn’t seem to do the job).
I now want to run a second stored procedure to update that temp table to add in the new columns. There will likely be several of these procedures and they likely will be shared in some cases. To this end in order to share them I would need to somehow tell it which ##table to update.
My current options seem to be:
I can pass this as a string and use dynamic sql but this seems messy.
I can enforce one stored procedure per client and then the table name (which is per client) can be hardcoded into the procedure. This seems like it has potential for non-optimal code (ie lots of very similar or even identical procedures).
Can anybody suggest any better options than these? currently I am looking at the second since there aren’t a huge amount of clients and this would seem to be the easiest way to do things. I’d like some better options though… I was hoping there may be some function or something that will take a string and get the table object of that name…
My final solution was just to refactor my code to make my core procedure return all columns that can be used for any client. This means that often it will return data that is not relevant (or is null). Also this method causes potential problems if a new client requires extra columns that some code may need updating if it assumes a given number of columns.
Its not a good solution unfortunately but its the one I went with for lack of other options.