Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Select from tables dynamically (table is variable)

$
0
0
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.

Viewing all articles
Browse latest Browse all 3145

Trending Articles