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

本文介绍如何使用 SQL 语句查询 SQL Server 中的所有数据库名称、特定数据库中的所有表名及表结构详情,以及如何获取表中的记录数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+&quo<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>"");
use master
select [name] from [sysdatabases] order by [name");

执行后,结果如下:
name                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------
A<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>br />TopWin
WeYyzyq
zt

(所影响的行数为 23 行)

     
我们通过查询其中一个数据库aspnetpager,就可以得到这个数据库中的所有的表名了,语句如下:

use aspnetpager
select [id][name] from [sysobjects] where [type]  
by [name]

执行后的结果如下:

id          name                                                              &nbs<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>bsp;            
----------- --------------------------------------------------------------------------------------------------------------------------------
629577281   BaseData
2137058649  dtproperties
2025058250  wqnews

(所影响的行数为 3 行)


我们通过上面的查询的id,可以查出这个表中所有的字段,例如我们查询BaseData表中所有的字段,就可以通过下面的语句查出:

select [name] from [syscolumns] where [order by [colid]
go

执行的结果如下:

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          &nbs<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>bsp;           11809
629577281   BaseData                                                                       11809

(所影响的行数为 2 行)

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值