數據庫添加注釋


sql server 添加表注釋、字段注釋
--為字段添加注釋
--格式如右: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 DISTINCT
d.name,
f.value
FROM
syscolumns a
LEFT JOIN systypes b ON a.xusertype= b.xusertype
INNER JOIN sysobjects d ON a.id= d.id
AND d.xtype= 'U'
AND d.name<> 'dtproperties'
LEFT JOIN syscomments e ON a.cdefault= e.id
LEFT JOIN sys.extended_properties g ON a.id= G.major_id
AND a.colid= g.minor_id
LEFT JOIN sys.extended_properties f ON d.id= f.major_id
AND f.minor_id= 0


--查看表的所有字段注釋
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM