0

I have an in-line function called myfunc and it returns a TABLE.

I can select certain columns from the function like so:

SELECT col1, col2, coln FROM myfunc()

However, is it possible to dynamically change the SELECT columns based upon a different table? We can assume the other table has all available column names (no typos, etc.)

Something like:

SELECT (SELECT available_cols FROM <other_table>) FROM myfunc()  --pseudo code

It looks like this can be achieved using the EXEC command but I'm curious if this can be baked into the SELECT command from the get-go.

Thank you.

NickBraunagel
  • 1,559
  • 1
  • 16
  • 30
  • 1
    You definitely need dynamic sql to do something like this. – Giorgos Betsos Dec 18 '17 at 15:31
  • 3
    SQL Server doesn't give you many opportunities to mix *metadata* and *data* together in a single query. Trying to do so usually means that the data model has become mixed up about the separation between those concepts. – Damien_The_Unbeliever Dec 18 '17 at 15:33

0 Answers0