SQL Server: Calculating column errors retrieved from a database?

I would like to request the definition of a computed column from the database, but cannot find a command that seems to do what I want ...

For example, if a column is defined as:

CallDT AS (CONVERT([datetime],dateadd(second,[StartDate],'01/01/1970'),(0))) 

in DDL, I would like to run a command in the database to get this AS statement so that I can compare it with its expected value. (I am developing an SQL parser that will compare an existing database with a DDL definition) ...

Is it possible?

+6
sql database sql-server calculated-columns
source share
2 answers

This works in SQL Server 2008

 create table dbo.Foo ( StartDate int, CallDT AS (CONVERT([datetime],dateadd(second,[StartDate],'01/01/1970'),(0))) ) select definition from sys.computed_columns where name='CallDT' and object_id=object_id('dbo.Foo') 
+13
source share

Try the following:

 SELECT name, definition FROM sys.computed_columns 

It should work in SQL Server 2005 and later.

+5
source share

All Articles