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

前端 未结 3 942
一个人的身影
一个人的身影 2020-12-01 18:06

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 
相关标签:
3条回答
  • 2020-12-01 18:16

    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;
    
    0 讨论(0)
  • 2020-12-01 18:18

    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')
    
    0 讨论(0)
  • 2020-12-01 18:21

    Use:

       SELECT so.name AS table_name, 
              sc.name AS column_name, 
              sm.text AS default_value
         FROM sys.sysobjects so
         JOIN sys.syscolumns sc ON sc.id = so.id
    LEFT JOIN sys.syscomments sm ON sm.id = sc.cdefault
        WHERE so.xtype = 'U' 
          AND so.name = @yourtable
     ORDER BY so.[name], sc.colid
    
    0 讨论(0)
提交回复
热议问题