【轉載】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 [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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章