How do I find a default constraint using INFORMATION_SCHEMA?

前端 未结 14 808
猫巷女王i
猫巷女王i 2020-12-04 13:40

I\'m trying to test if a given default constraint exists. I don\'t want to use the sysobjects table, but the more standard INFORMATION_SCHEMA.

I\'ve used this to che

14条回答
  •  死守一世寂寞
    2020-12-04 14:30

    The script below lists all the default constraints and the default values for the user tables in the database in which it is being run:

    SELECT  
            b.name AS TABLE_NAME,
            d.name AS COLUMN_NAME,
            a.name AS CONSTRAINT_NAME,
            c.text AS DEFAULT_VALUE
    FROM sys.sysobjects a INNER JOIN
            (SELECT name, id
             FROM sys.sysobjects 
             WHERE xtype = 'U') b on (a.parent_obj = b.id)
                          INNER JOIN sys.syscomments c ON (a.id = c.id)
                          INNER JOIN sys.syscolumns d ON (d.cdefault = a.id)                                          
     WHERE a.xtype = 'D'        
     ORDER BY b.name, a.name
    

提交回复
热议问题