Behrad Farsi
Behrad Farsi

Reputation: 1110

View with dynamic columns In SQL Server

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

Answers (2)

Mehrdad Bahrainy
Mehrdad Bahrainy

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

usr
usr

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

Related Questions