sql server 新增表註釋、欄位註釋
阿新 • • 發佈:2018-12-05
--為欄位添加註釋
--格式如右:execute sp_addextendedproperty 'MS_Description','欄位備註資訊','user','dbo','table','欄位所屬的表名','column','添加註釋的欄位名';
execute sp_addextendedproperty 'MS_Description','add by liyc. 診斷類別碼','user','dbo','table','DiagRecord','column','DiagTypeCode';
--修改欄位註釋
execute sp_updateextendedproperty 'MS_Description','add by liyc.','user','dbo','table','DiagRecord','column','DiagTypeCode';
--刪除欄位註釋
execute sp_dropextendedproperty 'MS_Description','user','dbo','table','DiagRecord','column','DiagTypeCode';
-- 新增表註釋
execute sp_addextendedproperty 'MS_Description','診斷記錄檔案','user','dbo','table','DiagRecord',null,null;
-- 修改表註釋
execute sp_updateextendedproperty 'MS_Description','診斷記錄檔案1','user','dbo','table','DiagRecord',null,null;
-- 刪除表註釋
execute sp_dropextendedproperty 'MS_Description','user','dbo','table','DiagRecord',null,null;
-- 說明:
-- 1.增加、修改、刪除註釋呼叫不同的儲存過程
-- 2.增加、修改註釋呼叫的儲存過程引數數量和含義相同,刪除備註比前兩者少了一個“備註內容”的引數
-- 3.為表添加註釋相比於為欄位添加註釋,最後兩個引數為null
--查看錶的註釋
select isnull(value,'') from sys.extended_properties ex_p where ex_p.minor_id=0
and ex_p.major_id in (select id from sys.sysobjects a where a.name='表名')
--查看錶的所有欄位註釋
SELECT [ColumnName] = [Columns].name ,
[Description] = [Properties].value,
[SystemTypeName] = [Types].name ,
[Precision] = [Columns].precision ,
[Scale] = [Columns].scale ,
[MaxLength] = [Columns].max_length ,
[IsNullable] = [Columns].is_nullable ,
[IsRowGUIDCol] = [Columns].is_rowguidcol ,
[IsIdentity] = [Columns].is_identity ,
[IsComputed] = [Columns].is_computed ,
[IsXmlDocument] = [Columns].is_xml_document
FROM sys.tables AS [Tables]
INNER JOIN sys.columns AS [Columns] ON [Tables].object_id = [Columns].object_id
INNER JOIN sys.types AS [Types] ON [Columns].system_type_id = [Types].system_type_id
AND is_user_defined = 0
AND [Types].name <> 'sysname'
LEFT OUTER JOIN sys.extended_properties AS [Properties] ON [Properties].major_id = [Tables].object_id
AND [Properties].minor_id = [Columns].column_id
AND [Properties].name = 'MS_Description'
WHERE [Tables].name ='表名' -- and [Columns].name = '欄位名'
ORDER BY [Columns].column_id