查詢表結構

2022-07-15 17:51:12 字數 4649 閱讀 6254

方法一

select

序號=c.column_id,

列名=c.name,

是否主鍵=isnull(idx.primarykey,n''),

資料型別=t.name,

長度=c.max_length,

precision=c.precision,

小數字=c.scale,

允許空=case when c.is_nullable=1 then n'是'else n'否' end,

預設值=isnull(d.definition,n''),

說明=isnull(pfd.[value],n'')

from sys.columns c

inner join sys.objects o

on c.[object_id]=o.[object_id]

and o.type='u'

and o.is_ms_shipped=0

inner join sys.types t

on c.user_type_id=t.user_type_id

left join sys.default_constraints d

on c.[object_id]=d.parent_object_id

and c.column_id=d.parent_column_id

and c.default_object_id=d.[object_id]

left join sys.extended_properties pfd

on pfd.class=1

and c.[object_id]=pfd.major_id

and c.column_id=pfd.minor_id

left join sys.extended_properties ptb

on ptb.class=1

and ptb.minor_id=0

and c.[object_id]=ptb.major_id

left join                       -- 索引及主鍵資訊

(select

idxc.[object_id],

idxc.column_id,

sort=case indexkey_property(idxc.[object_id],idxc.index_id,idxc.index_column_id,'isdescending')

when 1 then 'desc' when 0 then 'asc' else '' end,

primarykey=case when idx.is_primary_key=1 then n'是'else n'否' end,

indexname=idx.name

from sys.indexes idx

inner join sys.index_columns idxc

on idx.[object_id]=idxc.[object_id]

and idx.index_id=idxc.index_id

left join sys.key_constraints kc

on idx.[object_id]=kc.[parent_object_id]

and idx.index_id=kc.unique_index_id

inner join  -- 對於乙個列包含多個索引的情況,只顯示第1個索引資訊

(select [object_id], column_id, index_id=min(index_id)

from sys.index_columns

group by [object_id], column_id

) idxcuq

on idxc.[object_id]=idxcuq.[object_id]

and idxc.column_id=idxcuq.column_id

and idxc.index_id=idxcuq.index_id

) idx

on c.[object_id]=idx.[object_id]

and c.column_id=idx.column_id

where c.object_id= object_id('dbo.crm_商店')       -- 指定表

order by o.name,c.column_id

方法二select s.name    columname

,(case

when s.is_nullable=1 then n'是'

else n'否' end

) as    allownull

,s.max_length  maxlength

,typesystable.name datetype

from sys.columns s

inner join sys.types typesystable on s.system_type_id = typesystable.system_type_id

where s.object_id= object_id(@tablename)

and typesystable.name != 'sysname'

方法三(優化方法一)

select  s.column_id     序號

,s.name      列名稱

,isnull(idx.primarykey,n'否') 主鍵

,(case

when s.is_nullable=1 then n'是'

else n'否'

end) as      是否允許空

,s.max_length    長度

,typesystable.name   資料型別

,s.scale     小數字

,isnull(d.definition,n'') 預設值

,isnull(pfd.[value],n'') 說明

from sys.columns s

inner join sys.types typesystable on s.system_type_id = typesystable.system_type_id

inner join sys.objects o on s.[object_id]=o.[object_id]

and o.type='u'

and o.is_ms_shipped=0

left join sys.default_constraints d on s.[object_id]=d.parent_object_id

and s.column_id=d.parent_column_id

and s.default_object_id=d.[object_id]

left join sys.extended_properties pfd on pfd.class=1

and s.[object_id]=pfd.major_id

and s.column_id=pfd.minor_id

left join                       -- 索引及主鍵資訊

(   select

idxc.[object_id],

idxc.column_id,

sort=case indexkey_property(idxc.[object_id],idxc.index_id,idxc.index_column_id,'isdescending')

when 1 then 'desc' when 0 then 'asc' else '' end,

primarykey=case when idx.is_primary_key=1 then n'是'else n'否' end,

indexname=idx.name

from sys.indexes idx

inner join sys.index_columns idxc on idx.[object_id]=idxc.[object_id]

and idx.index_id=idxc.index_id

left join sys.key_constraints kc on idx.[object_id]=kc.[parent_object_id]

and idx.index_id=kc.unique_index_id

inner join  -- 對於乙個列包含多個索引的情況,只顯示第1個索引資訊

( select [object_id], column_id, index_id=min(index_id)

from sys.index_columns

group by [object_id], column_id

) idxcuq on idxc.[object_id]=idxcuq.[object_id]

and idxc.column_id=idxcuq.column_id

and idxc.index_id=idxcuq.index_id

) idx on s.[object_id]=idx.[object_id]

and s.column_id=idx.column_id

where s.object_id= object_id('crm_商店')

and typesystable.name != 'sysname'

order by o.name, s.column_id

查詢表結構

在日常生活中,幾乎每天都要進行一些查詢的工作,在 簿中查閱某個人的 號碼 在電腦的資料夾中查詢某個具體的檔案等等。本節主要介紹用於查詢操作的資料結構 查詢表。查詢表是由同一型別的資料元素構成的集合。例如 號碼簿和字典都可以看作是一張查詢表。一般對於查詢表有以下幾種操作 在查詢表中只做查詢操作,而不改...

查詢表結構

select case when a.colorder 1 then d.name else null end 表名,a.colorder 字段序號,a.name 欄位名,case when columnproperty a.id,a.name,isidentity 1 then else end ...

oracle 查詢表結構

通過資料字典來獲取,select table name,column name,data type,data length from user tab columns where table name not in select view name from user views and table...