Search This Blog

July 07, 2008

How to find columns with/without default values in a table

How to find the default values of all the columns in all the tables in a database.


SELECT SO.name AS [Table Name],SC.name AS [Column Name],SM.TEXT AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
INNER JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
UNION
SELECT SO.name AS [Table Name],SC.name AS [Column Name],'' AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
LEFT JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
AND SM.id IS NULL
ORDER BY SO.name,SC.colorder


How to find only the columns which contains default value in all the tables in a given database.

SELECT SO.name AS [Table Name],SC.name AS [Column Name],SM.TEXT AS [Default Value],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
INNER JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
ORDER BY SO.name,SC.colorder


How to find the columns and tables does not contain default values in a database.

SELECT SO.name AS [Table Name],SC.name AS [Column Name],SC.colorder AS [Column Order]
FROM sysobjects SO
INNER JOIN sys.syscolumns SC
ON SO.id = SC.id
LEFT JOIN sys.syscomments SM
ON SC.cdefault = SM.id
WHERE SO.xtype = 'U'
AND SM.id IS NULL
ORDER BY SO.name,SC.colorder

No comments: