如何使用SQL查询找出表中列的默认值?
使用此存储过程:
sp_columns @tablename
我获取了某些特定表的列的一些信息,但缺少列的默认值,我该如何获取它?
答案 0 :(得分:58)
您可以使用以下内容找到存储的定义(请记住调整列和表名称以找到与您的环境相关的名称!)
SELECT object_definition(default_object_id) AS definition
FROM sys.columns
WHERE name ='colname'
AND object_id = object_id('dbo.tablename')
答案 1 :(得分:15)
使用:
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
答案 2 :(得分:13)
我像这样使用INFORMATION_SCHEMA
表:
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;