今天整理資料時看到有這樣乙個查詢資料庫中的表和字段資訊的語句,很強! 就是忘了當初誰寫的了,印像中該是鄒建所創。也貼出來以後備用吧。
1select
2表名
=case
a.colorder
when
1then
c.name
else
''end,3
序
=a.colorder,
4欄位名
=a.name,
5標識
=case
columnproperty
(a.id,a.name,
'isidentity')
when
1then'√
'else
''end,6
主鍵
=case
7when
exists(8
select*9
from
sysobjects
10where
xtype='
pk'
andname in(
11select
name
12from
sysindexes
13whereid=
a.id
andindid in(
14select
indid
15from
sysindexkeys
16whereid=
a.id
andcolid in(
17select
colid
18from
syscolumns
19whereid=
a.id
andname
=a.name20)
21)22)
23)
24then'√
'25
else
''26
end,
27型別
=b.name,
28位元組數
=a.length,
29長度
=columnproperty
(a.id,a.name,
'precision'),
30小數
=case
isnull
(columnproperty
(a.id,a.name,
'scale'),
0)
31when
0then
''32
else
cast
(columnproperty
(a.id,a.name,
'scale')
asvarchar)33
end,
34允許空
=case
a.isnullable
when
1then'√
'else
''end,35
預設值
=isnull
(d.[
text],
''),
36說明
=isnull
(e.[
value],
'')
37from
syscolumns a
38left
join
systypes b
ona.xtype
=b.xusertype
39inner
join
sysobjects c
ona.id
=c.id
andc.xtype='
u'
andc.name
<>
'dtproperties'40
left
join
syscomments d
ona.cdefault
=d.id
41left
join
sysproperties e
ona.id
=e.id
anda.colid
=e.**allid
42order
byc.name, a.colorder
我修改一下,變個精簡版本的:12
select
a.name, b.xtype,b.name
3from
syscolumns a
4inner
join
systypes b5on
a.xtype
=b.xusertype
6inner
join
sysobjects c on7
a.id
=c.id
andc.xtype='
u'
andc.name
<>
'dtproperties
'where
c.name =表名
SQL語句建立字段,表,資料庫
sqlcommand cmd new sqlcommand alter table advisetable add name type conn sql語句建立字段 alter table advisetable add 欄位名 varchar 50 sql語句刪除字段 alter table us...
資料庫SQL語句查詢
新手小白菜一枚,求知 查詢所有同學的學號 姓名 選課數 總成績 select t1.stuid,t1.stuname,count t2.courseid sum score from tblstudent t1,tblscore t2 where t1.stuid t2.stuid group by...
如何使用sql語句查詢資料庫的表中字段數目
1 oracle select count from user tab cols where table name 表名 注意 表名中的英文本母大寫 2 mysql select count from information schema.columns where table name 表名 注意...