您的位置:首页 > 数据库

SQL Server中查询所有数据库、所有表、字段的Sql语句

2007-08-01 09:38 936 查看
google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");
当我们建立Sql Server 连接之后,可以通过如下语句得到当前Sql server中所有的数据的名称: google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");


use master


select [name] from [sysdatabases] order by [name]


go

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");

执行后,结果如下:
name
--------------------------------------------------------------------------------------------------------------------------------
AspNetPager
ciw_sign
COMA_SAMSUNG
DJWorldProject
EmoAsp
iParkBJDatabase
iParkDB
jscp
master
model
msdb
MSPetShop
MSPetShopOrders
MYCLUB
Northwind
OnlinePro
pubs
SamSungShopAll
SoundEquipment
tempdb
TopWin
WeYyzyq
zt

(所影响的行数为 23 行)

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");
我们通过查询其中一个数据库aspnetpager,就可以得到这个数据库中的所有的表名了,语句如下:


use aspnetpager


select [id], [name] from [sysobjects] where [type] = 'u' order by [name]

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");

执行后的结果如下:

id name
----------- --------------------------------------------------------------------------------------------------------------------------------
629577281 BaseData
2137058649 dtproperties
2025058250 wqnews

(所影响的行数为 3 行)

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");
我们通过上面的查询的id,可以查出这个表中所有的字段,例如我们查询BaseData表中所有的字段,就可以通过下面的语句查出:


select [name] from [syscolumns] where [id] = 629577281 order by [colid]


go

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");

执行的结果如下:

name
--------------------------------------------------------------------------------------------------------------------------------
app_id
app_bscnm
app_shop_type
app_money
app_citylev
app_protype
app_type
SUBS_NM
AddDate
FinishDate
CheckDate
AsFinishDate
FromTable

(所影响的行数为 13 行)

通过表basedata所在[sysobjects]中对应的id“629577281”,可以得到该表中的记录条数。前提这个表中要建立索引,语句如下:


select [o].[id], [o].[name], [i].[rows] from [sysindexes] [i], [sysobjects] [o] where [i].[first]>0 and [o].[id]=[i].[id] and [o].[type]='u' and [o].[id]=629577281

执行的结果如下:
id name rows
----------- ------------------------------------------------------------------------- -----------
629577281 BaseData 11809
629577281 BaseData 11809

(所影响的行数为 2 行)

希望这些对于一些人有所帮助。当然重要的是留下来以供自己参考。

google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: