Reputation: 1110
How can i create a view in SQL Server,where columns are defined and appear according to a value stored in another table.These columns are almost identically calculated by a function but their number is set from another table's record.
Upvotes: 4
Views: 2255
Reputation: 1617
You can create an inline Table-valued function and define your fields and return the table then call the function inside your view.
Upvotes: 0
Reputation: 171246
This is not possible with a view because SQL Server queries are always statically typed in the sense that both column count, names and types are statically known at execution time.
You need dynamic SQL for dynamic columns.
Views do not support dynamic SQL. You have to find some other way of returning the data, maybe with one row per logical column.
Upvotes: 5