查詢表結構資訊

2021-07-11 14:26:55 字數 4272 閱讀 7718

--

查詢表結構資訊

select

tablename

=case

when

c.column_id=1

then

o.name

elsen''

end,

tabledesc

=isnull

(case

when

c.column_id=1

then

ptb.

[value

]end

,n''

),column_id

=c.column_id,

columnname

=c.name,

primarykey

=isnull

(idx.primarykey,n

''),

[identity]=

case

when

c.is_identity=1

thenn'

√'elsen''

end,

computed

=case

when

c.is_computed=1

thenn'

√'elsen''

end,

type

=t.name,

length

=c.max_length,

precision=c.

precision

,scale

=c.scale,

nullable

=case

when

c.is_nullable=1

thenn'

√'elsen''

end,

[default]=

isnull

(d.definition,n

''),

columndesc

=isnull

(pfd.

[value],n

''),

indexname

=isnull

(idx.indexname,n

''),

indexsort

=isnull

(idx.sort,n

''),

create_date

=o.create_date,

modify_date

=o.modify_date

from

sys.columns c

inner

join

sys.objects o

onc.

[object_id]=

o.[object_id

]and

o.type='

u'ando.is_ms_shipped=0

inner

join

sys.types t

onc.user_type_id

=t.user_type_id

left

join

sys.default_constraints d

onc.

[object_id]=

d.parent_object_id

andc.column_id

=d.parent_column_id

andc.default_object_id=d.

[object_id

]left

join

sys.extended_properties pfd

onpfd.class=1

andc.

[object_id]=

pfd.major_id

andc.column_id

=pfd.minor_id

--and pfd.name='caption'  -- 字段說明對應的描述名稱(乙個字段可以新增多個不同name的描述)

left

join

sys.extended_properties ptb

onptb.class=1

andptb.minor_id=0

andc.

[object_id]=

ptb.major_id

--and pfd.name='caption'  -- 表說明對應的描述名稱(乙個表可以新增多個不同name的描述)

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

1then

'desc

'when

0then

'asc

'else

''end

,primarykey

=case

when

idx.is_primary_key=1

thenn'

√'elsen''

end,

indexname

=idx.name

from

sys.indexes idx

inner

join

sys.index_columns idxc

onidx.

[object_id]=

idxc.

[object_id

]and

idx.index_id

=idxc.index_id

left

join

sys.key_constraints kc

onidx.

[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

groupby[

object_id

], column_id

) idxcuq

onidxc.

[object_id]=

idxcuq.

[object_id

]and

idxc.column_id

=idxcuq.column_id

andidxc.index_id

=idxcuq.index_id

) idx

onc.

[object_id]=

idx.

[object_id

]and

c.column_id

=idx.column_id

--where o.name=n'要查詢的表'       -- 如果只查詢指定表,加上此條件

order

byo.name,c.column_id --

獲取庫中所有表及每個表的記錄數

declare

@sql

varchar

(8000

)set

@sql='

select * from (

'select

@sql

=@sql+'

select name =

'''+

name

+'''

, count(*) as num from ['+

name+'

] union all

'from

sysobjects

where

xtype='

u'set@sql

=left

(@sql

,len

(@sql) -

10) +'

)a'exec

(@sql

)

查詢表結構資訊

查詢表結構資訊 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,標識 ca...

查詢Sqlserver 表結構資訊 SQL

select 表名 case when a.colorder 1then d.name else end 表說明 case when a.colorder 1then isnull f.value,else end 字段序號 a.colorder,欄位名 a.name,標識 case when co...

查詢表資訊

參考 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 wh...