Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve column default value from MS SQL data table

I am using DataAdapter.FillSchema to retrieve tables' schema from MS SQL. Unfortunately this doesn't return the default value for the columns. Is there a way to retrieve this value as part of the schema in a fast and efficient way as I need to examine hundreds of tables?

Thanks!

like image 942
Daniel Avatar asked Apr 25 '12 11:04

Daniel


2 Answers

Default value is determined at the time of row insertion only.

As an alternative, you can utilize Information_schema

SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
FROM AdventureWorks2012.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Person';
like image 143
Adil Avatar answered Oct 19 '22 04:10

Adil


Try the following query:

SELECT object_definition(default_object_id) AS definition
FROM   sys.columns
WHERE  name      ='ColumnName'
AND    object_id = object_id('TableName')
like image 34
mannu2050 Avatar answered Oct 19 '22 02:10

mannu2050