獲取MSSQL 表結構中字段的備註 主鍵等資訊

2021-06-18 06:18:15 字數 3098 閱讀 3009

1、mssql2000

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

sysproperties g

on a.id=g.id and a.colid=g.smallid  

left join

sysproperties f

on d.id=f.id and f.smallid=0

where

d.name='fi_dept'    --如果只查詢指定表,加上此條件

order by

a.id,a.colorder

2、mssql2005

use test--資料庫

go--2005實現字段屬性統計(2000裡的系統表sysproperties描述表、欄位不存在,2005裡用sys.extended_properties檢視替代)

select

[表名]=c.name,

[表說明]=isnull(f.[value],''),

[列名]=a.name,

[列序號]=a.column_id,

[標識]=case when is_identity=1 then '√' else '' end,

[主鍵]=case when exists(select 1 from sys.objects where parent_object_id=a.object_id and type=n'pk' and name in

(select name from sys.indexes where index_id in

(select indid from sysindexkeys where and colid=a.column_id)))

then '√' else '' end,

[型別]=b.name,

[位元組數]=case when a.[max_length]=-1 and b.name!='xml' then 'max/2g'

when b.name='xml' then ' 2^31-1位元組/2g'

else rtrim(a.[max_length]) end,

[長度]=columnproperty(a.object_id,a.name,'precision'),

[小數]=isnull(columnproperty(a.object_id,a.name,'scale'),0),

[是否為空]=case when a.is_nullable=1 then '√' else '' end,

[列說明]=isnull(e.[value],''),

[預設值]=isnull(d.text,'')   

from

sys.columns a

left join

sys.types b on a.user_type_id=b.user_type_id

inner join

sys.objects c on a.object_id=c.object_id and c.type='u'

left join

syscomments d on a.default_object_id=d.id

left join

sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1

left join

sys.extended_properties f on f.major_id=c.object_id and f.minor_id=0 and f.class=1

結果:

MSSQL 獲取所有表及字段定義

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 ...

獲取oracle表結構的字段資訊

select a.column id as 列號,a.column name as 列名,a.data type as 型別,decode a.data type,number a.data precision,a.data length as 長度,a.data scale as 小數字,deco...

MSSQL2005查詢表中字段的描述

自定義查詢的功能是使用者可以選擇資料庫中表和表中的字段,但一般欄位都使用英文本元表示,這樣對於使用者來說根本無法理解表中字段的含義,解決辦法一般有兩種 1.向資料庫中增加兩個表,乙個存放庫中的資料表,另乙個對應表中的字段。使用時只要增加相關的表和字段的條目和注釋就可以了。2.另一種方法是從資料庫中查...