Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Find out default value of a column with a query

How can I find out the default value of a column in a table using a SQL query?

By using this stored procedure:

sp_columns @tablename  

I get some information on the columns of a particular table but the default value of the columns is missing, How can I get it?

like image 285
zad Avatar asked Sep 29 '10 00:09

zad


People also ask

What is the default with of a column?

Answer: The default size of an Excel column is 8.43, which correlates to 64 pixels. Rows can have a maximum height of 409. This number represents how many one-seventy seconds of an inch the row can hold.

Can you define a default value for a column?

Select the column for which you want to specify a default value. In the Column Properties tab, enter the new default value in the Default Value or Binding property. To enter a numeric default value, enter the number. For an object or function enter its name.

Which constraint assigns a default value for a column?

The column default constraint is used to ensure that a given column of a table is always assigned a predefined value whenever a row that does not have a specific value for that column is added to the table.


2 Answers

You can find the stored definition with the below (remember to adjust the column and table name to find to be ones relevant to your environment!)

SELECT object_definition(default_object_id) AS definition FROM   sys.columns WHERE  name      ='colname' AND    object_id = object_id('dbo.tablename') 
like image 53
Martin Smith Avatar answered Oct 07 '22 05:10

Martin Smith


I use INFORMATION_SCHEMA table like this:

SELECT      TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT FROM     INFORMATION_SCHEMA.COLUMNS WHERE   TABLE_SCHEMA = @SchemaName    AND TABLE_NAME = @TableName   AND COLUMN_NAME = @ColumnName; 
like image 24
shA.t Avatar answered Oct 07 '22 05:10

shA.t