Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get List of Computed Columns in Database Table (SQL Server)

Would any of you know how to get the list of computed columns in a SQL Server database table?

I found sys.sp_help tablename does return this information, but only in the second result-set.

I am trying to find out if there is a better way of doing this. Something which only returns a single result set.

like image 471
Gia Avatar asked Sep 27 '09 18:09

Gia


People also ask

What are computed columns in SQL?

A computed column is a virtual column that is not physically stored in the table, unless the column is marked PERSISTED. A computed column expression can use data from other columns to calculate a value for the column to which it belongs.

How do I get a list of columns in all tables?

MyTable) and hit ALT + F1 , you'll get a list of column names, type, length, etc.


1 Answers

Check the sys.columns system catalog view:

SELECT * FROM sys.columns WHERE is_computed = 1 

This gives you all computed columns in this database.

If you want those for just a single table, use this query:

SELECT * FROM sys.columns WHERE is_computed = 1 AND object_id = OBJECT_ID('YourTableName') 

This works on SQL Server 2005 and up.

UPDATE: There's even a sys.computed_columns system catalog view which also contains the definition (expression) of the computed column - just in case that might be needed some time.

SELECT * FROM sys.computed_columns WHERE object_id = OBJECT_ID('YourTableName') 
like image 183
marc_s Avatar answered Oct 21 '22 14:10

marc_s