獲取MSSQL 表結(jié)構(gòu)中字段的備注、主鍵等信息的sql
更新時(shí)間:2013年09月09日 10:28:33 作者:
本文為大家詳細(xì)介紹下如何獲取MSSQL 表結(jié)構(gòu)中字段的備注、主鍵等信息,感興趣的朋友可以參考下
1、MSSQL2000
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序號(hào) = a.colorder,
字段名 = a.name,
標(biāo)識(shí) = 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,
占用字節(jié)數(shù) = a.length,
長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小數(shù)位數(shù) = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允許空 = case when a.isnullable=1 then '√'else '' end,
默認(rèn)值 = 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--數(shù)據(jù)庫
go
--2005實(shí)現(xiàn)字段屬性統(tǒng)計(jì)(2000里的系統(tǒng)表sysproperties描述表、字段不存在,2005里用sys.extended_properties視圖替代)
select
[表名]=c.Name,
[表說明]=isnull(f.[value],''),
[列名]=a.Name,
[列序號(hào)]=a.Column_id,
[標(biāo)識(shí)]=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,
[字節(jié)數(shù)]=case when a.[max_length]=-1 and b.Name!='xml' then 'max/2G'
when b.Name='xml' then ' 2^31-1字節(jié)/2G'
else rtrim(a.[max_length]) end,
[長度]=ColumnProperty(a.object_id,a.Name,'Precision'),
[小數(shù)]=isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0),
[是否為空]=case when a.is_nullable=1 then '√' else '' end,
[列說明]=isnull(e.[value],''),
[默認(rèn)值]=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
[/code]
結(jié)果:
復(fù)制代碼 代碼如下:
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序號(hào) = a.colorder,
字段名 = a.name,
標(biāo)識(shí) = 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,
占用字節(jié)數(shù) = a.length,
長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小數(shù)位數(shù) = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允許空 = case when a.isnullable=1 then '√'else '' end,
默認(rèn)值 = 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--數(shù)據(jù)庫
go
--2005實(shí)現(xiàn)字段屬性統(tǒng)計(jì)(2000里的系統(tǒng)表sysproperties描述表、字段不存在,2005里用sys.extended_properties視圖替代)
select
[表名]=c.Name,
[表說明]=isnull(f.[value],''),
[列名]=a.Name,
[列序號(hào)]=a.Column_id,
[標(biāo)識(shí)]=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,
[字節(jié)數(shù)]=case when a.[max_length]=-1 and b.Name!='xml' then 'max/2G'
when b.Name='xml' then ' 2^31-1字節(jié)/2G'
else rtrim(a.[max_length]) end,
[長度]=ColumnProperty(a.object_id,a.Name,'Precision'),
[小數(shù)]=isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0),
[是否為空]=case when a.is_nullable=1 then '√' else '' end,
[列說明]=isnull(e.[value],''),
[默認(rèn)值]=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
[/code]
結(jié)果:
相關(guān)文章
SQL Agent服務(wù)無法啟動(dòng)的解決方法
SQL Agent服務(wù)無法啟動(dòng)怎么辦?這篇文章主要介紹了SQL Agent服務(wù)無法啟動(dòng)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
淺析SQL Server中包含事務(wù)的存儲(chǔ)過程
數(shù)據(jù)庫事務(wù)(Database Transaction) ,是指作為單個(gè)邏輯工作單元執(zhí)行的一系列操作,要么完整地執(zhí)行,要么完全地不執(zhí)行。那么在存儲(chǔ)過程里添加事務(wù),則可以保證該事務(wù)里的所有sql代碼要么完全執(zhí)行要么完全不執(zhí)行。2014-08-08
sql語句將數(shù)據(jù)庫一條數(shù)據(jù)通過分隔符切割成多列方法實(shí)例
最近工作中遇到數(shù)據(jù)表中有一列數(shù)據(jù),然后需要將該列數(shù)據(jù)分成三列,下面這篇文章主要給大家介紹了關(guān)于sql語句將數(shù)據(jù)庫一條數(shù)據(jù)通過分隔符切割成多列的相關(guān)資料,需要的朋友可以參考下2023-03-03
Navicat 連接SQLServer數(shù)據(jù)庫(圖文步驟)
這篇文章主要介紹了Navicat 連接SQLServer數(shù)據(jù)庫(圖文步驟),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
sql?server如何去除數(shù)據(jù)中的一些無用的空格
這篇文章主要介紹了sql?server去除數(shù)據(jù)中的一些無用的空格,本文給大家提到了一些常用的函數(shù),結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
50個(gè)常用sql語句 網(wǎng)上流行的學(xué)生選課表的例子
這篇文字在網(wǎng)上被轉(zhuǎn)載爛了,里面有些sql適合用在應(yīng)用系統(tǒng)里,有些“報(bào)表”的感 覺更重些,主要是想復(fù)習(xí)前者2012-06-06

