擴展屬性 (MS_Description) 是 SQL Server 官方推薦的**數據字典**存儲機制,將業務含義直接掛在表/列/索引等對象上,隨庫遷移、版本控制、工具讀取零成本。
| 操作 | 存儲過程 | 關鍵參數 |
|---|---|---|
| 添加 | sp_addextendedproperty | @name, @value, @level0type, @level0name, @level1type, @level1name, @level2type, @level2name |
| 修改 | sp_updateextendedproperty | 同添加 |
| 刪除 | sp_dropextendedproperty | 同添加 (無 @value) |
-- 添加列描述
EXEC sys.sp_addextendedproperty
@name = N'MS_Description',
@value = N'客戶郵箱地址,唯一索引',
@level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = N'Customers',
@level2type = N'COLUMN', @level2name = N'Email';
GO
-- 修改描述
EXEC sys.sp_updateextendedproperty
@name = N'MS_Description',
@value = N'客戶郵箱,登錄帳號,唯一',
@level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = N'Customers',
@level2type = N'COLUMN', @level2name = N'Email';
GO
-- 刪除描述
EXEC sys.sp_dropextendedproperty
@name = N'MS_Description',
@level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = N'Customers',
@level2type = N'COLUMN', @level2name = N'Email';
GO
SELECT *
FROM fn_listextendedproperty (NULL, 'SCHEMA', 'dbo', 'TABLE', 'Customers', 'COLUMN', DEFAULT)
WHERE objname = 'Email';
SELECT
SCHEMA_NAME(o.schema_id) AS SchemaName,
o.name AS TableName,
c.name AS ColumnName,
ep.value AS Description
FROM sys.extended_properties ep
INNER JOIN sys.objects o ON ep.major_id = o.object_id
INNER JOIN sys.columns c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE ep.class = 1 -- 列級
AND ep.name = 'MS_Description'
ORDER BY SchemaName, TableName, ColumnName;
SELECT
SCHEMA_NAME(t.schema_id) AS SchemaName,
t.name AS TableName,
t.type_desc AS TableType,
ISNULL(tep.value, '') AS TableDescription,
c.column_id AS ColOrder,
c.name AS ColumnName,
ty.name AS DataType,
c.max_length AS MaxLen,
c.is_nullable AS Nullable,
c.is_identity AS Identity,
ISNULL(cep.value, '') AS ColumnDescription
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
LEFT JOIN sys.extended_properties tep
ON tep.major_id = t.object_id AND tep.minor_id = 0 AND tep.class = 1 AND tep.name = 'MS_Description'
LEFT JOIN sys.extended_properties cep
ON cep.major_id = c.object_id AND cep.minor_id = c.column_id AND cep.class = 1 AND cep.name = 'MS_Description'
WHERE t.is_ms_shipped = 0
ORDER BY SchemaName, TableName, ColOrder;
| 層級 | levelNtype | levelNname 示例 |
|---|---|---|
| 0 | SCHEMA / USER | dbo |
| 1 | TABLE / VIEW / PROCEDURE / FUNCTION / TRIGGER / DEFAULT / RULE | Customers |
| 2 | COLUMN / INDEX / CONSTRAINT / PARAMETER / TRIGGER | Email / PK_Customers |
最多 3 層 (0/1/2),覆蓋所有主要對象。
MS_Description,工具 (SSMS/Redgate/Dataedo/自研) 均識別此名。{"zh-CN":"...","en-US":"..."},前端按語系解析。