以前在SQL SERVER建表时,总看到扩展属性,但一直未使用过。今天研究下:
增加扩展属性:
语法:
sp_addextendedproperty
[ @name = ] { 'property_name' }
[ , [ @value = ] { 'value' }
[ , [ @level0type = ] { 'level0_object_type' }
, [ @level0name = ] { 'level0_object_name' }
[ , [ @level1type = ] { 'level1_object_type' }
, [ @level1name = ] { 'level1_object_name' }
[ , [ @level2type = ] { 'level2_object_type' }
, [ @level2name = ] { 'level2_object_name' }
]
]
]
]
注明:为了指定扩展属性,将 SQL Server 数据库中的对象分为三个级别:0、1 和 2。级别 0 是*别,该级别被定义为在数据库作用域包含的对象。级别 1 的对象包含在架构作用域或用户作用域中,而级别 2 的对象包含在级别 1 对象中。可以为这些级别中任一级别的对象定义扩展属性。
将扩展属性添加到数据库中
EXEC sp_addextendedproperty
@name = N'Caption', @value = ' OLTP Database';
将输入掩码属性添加到列中
EXEC sp_addextendedproperty
@name= N'Input Mask ', @value='99999 or 99999-9999 or #### ###',
@level0type= N'Schema', @level0name= Person,
@level1type= N'Table', @level1name= Address,
@level2type= N'Column',@level2name= PostalCode;
将扩展属性添加到表中的列
EXEC sp_addextendedproperty
@name = N'Caption', @value = 'Postal code is a required column.',
@level0type = N'Schema', @level0name = dbo,
@level1type = N'Table', @level1name = Address,
@level2type = N'Column', @level2name = PostalCode;
将扩展属性添加到文件组中
EXEC sys.sp_addextendedproperty
@name = N'MS_DescriptionExample',
@value = N'Primary filegroup for the AdventureWorks sample database.',
@level0type = N'FILEGROUP', @level0name = [PRIMARY];
查看扩展属性:
SELECT major_id, minor_id, t.name AS [Table Name], c.name AS [Column Name], value AS [Extended Property]
FROM sys.extended_properties AS ep
INNER JOIN sys.tables AS t ON ep.major_id = t.object_id
INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE class = 1;
查看数据库级别的扩展属性
SELECT objtype, objname, name, value
FROM fn_listextendedproperty(default, default, default, default, default, default, default);
查看某个架构中所有表的扩展属性
SELECT objtype, objname, name, value
FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', default, NULL, NULL);