I have the case where I need to perform a query over a table that i do not know its name at design time.
For example, I have the below tables:
Table_Items_1
Table_Items_2
Table_Items_3
Table_Items_4
Table_Items_5
I need to select depending on a number "x" which refers to the suffix of the table. (Number of tables is dynamic, I cannot do if-else).
This can be done using dynamic SQL. However, I need to have it more generic, since it will be referenced by several other functions/SPs.
I was thinking of Table Valued Functions, since I need to perform other queries over the above query (i.e.: inner joins, other select queries that depend on the result or fields etc.) but Table valued functions do not accept calling stored procedures nor dynamic SQL.
P.S.: I do not mind changing the method, TVF are not a must, yet, I need something generic that can be references from other stored procedures/functions.
Any solution?
Thank you.
What I have tried:
dynamic sql, table valued functions, stored procedure being called from tvf, tvf that contains dynamic sql