select
欄位名=c.name,
字段說明=isnull(pfd.[value],n''),
型別=t.name,
長度=c.max_length,
預設值=isnull(d.definition,n''),
允許空=case when c.is_nullable=1 then n'√'else n'' end,
isnull(idx.primarykey,'') as 主鍵
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
-- and pfd.name='caption' -- 字段說明對應的描述名稱(乙個字段可以新增多個不同name的描述)
left join sys.extended_properties ptb on ptb.class=1 and ptb.minor_id=0 and c.[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 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 o.name=n'hem_factory' -- 如果只查詢指定表,加上此條件
order by o.name,c.column_id
匯出表結構
select 表名 case when a.colorder 1 then d.name else end,字段序號 a.colorder,欄位名 a.name,字段說明 isnull g.value 型別 b.name,占用位元組數 a.length,長度 columnproperty a.id,...
匯出表結構
資料庫遷移時,匯出表結構,用設計工具匯入 1 select 欄位名 a.name,名稱 a.name,型別 case when b.name int then 整數 when b.name ntext then 文字 when b.name date then 日期 when b.name smal...
Oracle 匯出表結構
分析oracle下匯出某使用者所有表的方法 可能很多使用oracle的客戶都會遇到想把某使用者所有表匯出的情況,本文就提供這樣乙個方法幫你輕鬆解決這個問題。首先在sqlplus下以該使用者登入到oracle資料庫,然後將以下內容貼上到sqlplus中 set feedback off set pag...