获取默认约束信息

时间:2011-11-07 19:07:59

标签: sql sql-server sql-server-2008 tsql

我正在尝试获取给定表的Default Constraints信息。我写了这样的话:

SELECT  c.name ,
        col.name
FROM    Rem.sys.default_constraints c
        INNER JOIN Rem.sys.columns col ON col.default_object_id = c.object_id
        INNER JOIN Rem.sys.objects o ON o.object_id = c.parent_object_id
        INNER JOIN Rem.sys.schemas s ON s.schema_id = o.schema_id       
WHERE   s.name = 'dbo'
        AND o.name = 'Desk_Hist'

但它不会返回默认值。所以我尝试了另一种方式,查询是:

SELECT *
                FROM    Rem.information_schema.columns columns
                WHERE   columns.table_catalog = 'Rem'
                        AND columns.table_schema = 'dbo'
                        AND columns.table_name = 'Desk_Hist'
                        AND COLUMN_DEFAULT IS NOT NULL  

但它没有给我任何回报。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:4)

默认值位于“定义”列中,尝试运行不带位置的查询:

SELECT  c.name , 
        col.name, 
        c.definition
FROM    Rem.sys.default_constraints c 
        INNER JOIN Rem.sys.columns col ON col.default_object_id = c.object_id 
        INNER JOIN Rem.sys.objects o ON o.object_id = c.parent_object_id 
        INNER JOIN Rem.sys.schemas s ON s.schema_id = o.schema_id 

答案 1 :(得分:1)

您的第一个查询应该有效,但您只能在约束表(c.name)中包含约束名称,而不包含任何其他内容。

尝试在选择列表中加入c.definition