這周整理了數據庫文檔,發現用導出腳原本整理表的信息註釋查看不方便,所以我就想能不能SQL語句查詢表的註釋或者表的字段。我就我問朋友是否是能夠,他給我點指導,而後本身也在網上百度,來實現本身的想法,我把我本身查找的資料總結,理解,彙總的隨筆,如文章中有不足的地方,請你們多多指教;或者文章內容與他人類似,望見諒。sql
information_schema:系統視圖數據庫
sys.extended_properties:系統視圖sqlserver
表或表字段等的註釋,是數據庫對象的擴展屬性。在MSSQL中,支持把一些註釋性的內容放到數據庫或數據庫對象中,加強可讀性,有助於往後的管理和維護工做。擴展屬性的內容能夠經過SSMS添加、修改或刪除,也能夠經過系統視圖查詢,經過執行相關的存儲過程來維護。spa
1. 查詢一個表的全部列名code
------查詢一個表的全部列名
Select Name FROM SysColumns Where id=Object_Id('Sys_User') select t.column_name from information_schema.columns t where t.table_name='Sys_User';
如圖所示:orm
2. 查詢一個表的全部字段的註釋server
------查詢一個表的全部字段的註釋
select value from sys.extended_properties where major_id = object_id ('Sys_User' );
如圖所示:對象
3. 查詢一個表的全部列名,字段的註釋blog
------查詢一個表的全部列名,字段的註釋
SELECT A.name AS table_name, B.name AS column_name, C.value AS column_description FROM sys.tables A INNER JOIN sys.columns B ON B.object_id = A.object_id LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id WHERE A.name = 'Sys_User'
如圖所示:ip
4. 查詢一個表的全部列名,數據類型
------查詢一個表的全部列名,數據類型
select column_name,data_type from information_schema.columns where table_name = 'Sys_User'
如圖所示:
5. 查詢一個表的全部信息
------查詢一個表的全部信息
select * FROM information_schema.columns where table_name = 'Sys_User'
6. sqlserver 查詢某個表的列名稱、說明、備註、類型等
------sqlserver 查詢某個表的列名稱、說明、備註、類型等
SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序號 = a.colorder, 字段名 = a.name, 標識 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 主鍵 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end, 類型 = b.name, 佔用字節數 = a.length, 長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小數位數 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 容許空 = case when a.isnullable=1 then '√'else '' end, 默認值 = isnull(e.text,''), 字段說明 = isnull(g.[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 where d.name='Sys_User' --若是隻查詢指定表,加上此where條件,tablename是要查詢的表名;去除where條件查詢全部的表信息 order by a.id,a.colorder
如圖所示: