怎么查询某个数据库中的某些表共有多少条数据 sqlserver2005
6个回答
展开全部
--代码有点多,应该是一个存储过程才能处理过来,
------------------------------------------------------------------
use master
go
EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'Objects of type %s do not have space allocated.',
@lang = 'us_english',@replace = 'replace'
EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'没有为类型 %1! 的对象分配的空间。',
@lang = '简体中文',@replace = 'replace'
go
if exists (select 1 from sysobjects where name = 'sp_spaceused2' and type = 'P')
drop procedure sp_spaceused2
go
create procedure sp_spaceused2 (
@type varchar(2) = 'U', -- 对象类型.
@updateusage varchar(5) = false, -- Param. for specifying that
-- usage info. should be updated.
@name varchar(255) --查询的表名,如果查询全部请传递'%'
)
as
create table #spt_space
(
id int not null primary key,
name sysname,
rows int null,
reserved dec(15) null,
data dec(15) null,
indexp dec(15) null,
unused dec(15) null
)
/*
** Check the object type.
*/
if @type not in ('U','S') -- no physical data storage.
begin
raiserror(55000,-1,-1,@type)
return (1)
end
/*
** Check to see if user wants usages updated.
*/
if @updateusage is not null
begin
select @updateusage=lower(@updateusage)
if @updateusage not in ('true','false')
begin
raiserror(15143,-1,-1,@updateusage)
return(1)
end
end
if @updateusage = 'true'
begin
dbcc updateusage(0) with no_infomsgs
print ' '
end
set nocount on
--id, name, rows
insert into #spt_space (id, name, rows, data)
select i.id, o.name, i.rows, 0
from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid < 2 and o.type = @type and o.name like @name
--reserved
update #spt_space
set reserved = r.reserved
from ( select o.id, sum(i.reserved) as reserved from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--data
update #spt_space
set data = data + r.pages
from ( select o.id, isnull(sum(i.dpages),0) as pages from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid <2 and o.type = @type group by o.id) r
where #spt_space.id = r.id
update #spt_space
set data = data + r.used
from ( select o.id, isnull(sum(used), 0) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid = 255 and o.type = @type group by o.id) r
where #spt_space.id = r.id
--index page
update #spt_space
set indexp = r.used - data
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--unused page
update #spt_space
set unused = reserved - r.used
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--output
select #spt_space.name,
rows = convert(char(11), rows),
reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
' ' + 'KB'),
data = ltrim(str(data * d.low / 1024.,15,0) +
' ' + 'KB'),
index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
' ' + 'KB'),
unused = ltrim(str(unused * d.low / 1024.,15,0) +
' ' + 'KB')
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = 'E' order by len(ltrim(rtrim(reserved))) desc,reserved desc
go
----------------------------------------------------------------
--2、设置为系统存储过程
--sp_MS_marksystemobject 'sp_spaceused2'
--3、第三步,就可以查询了。
exec sp_spaceused2 'U',false,'要查询的表名'
go
--
--执行效果如下:
--name rows(这个就是每个表的行数)
--spt_values 2508 208 KB 96 KB 88 KB 24 KB
--MSreplication_options 3 16 KB 8 KB 8 KB 0 KB
-------------------------------------------------------
--以上测试在SQL2005以上版本中可以运行
------------------------------------------------------------------
use master
go
EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'Objects of type %s do not have space allocated.',
@lang = 'us_english',@replace = 'replace'
EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'没有为类型 %1! 的对象分配的空间。',
@lang = '简体中文',@replace = 'replace'
go
if exists (select 1 from sysobjects where name = 'sp_spaceused2' and type = 'P')
drop procedure sp_spaceused2
go
create procedure sp_spaceused2 (
@type varchar(2) = 'U', -- 对象类型.
@updateusage varchar(5) = false, -- Param. for specifying that
-- usage info. should be updated.
@name varchar(255) --查询的表名,如果查询全部请传递'%'
)
as
create table #spt_space
(
id int not null primary key,
name sysname,
rows int null,
reserved dec(15) null,
data dec(15) null,
indexp dec(15) null,
unused dec(15) null
)
/*
** Check the object type.
*/
if @type not in ('U','S') -- no physical data storage.
begin
raiserror(55000,-1,-1,@type)
return (1)
end
/*
** Check to see if user wants usages updated.
*/
if @updateusage is not null
begin
select @updateusage=lower(@updateusage)
if @updateusage not in ('true','false')
begin
raiserror(15143,-1,-1,@updateusage)
return(1)
end
end
if @updateusage = 'true'
begin
dbcc updateusage(0) with no_infomsgs
print ' '
end
set nocount on
--id, name, rows
insert into #spt_space (id, name, rows, data)
select i.id, o.name, i.rows, 0
from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid < 2 and o.type = @type and o.name like @name
--reserved
update #spt_space
set reserved = r.reserved
from ( select o.id, sum(i.reserved) as reserved from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--data
update #spt_space
set data = data + r.pages
from ( select o.id, isnull(sum(i.dpages),0) as pages from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid <2 and o.type = @type group by o.id) r
where #spt_space.id = r.id
update #spt_space
set data = data + r.used
from ( select o.id, isnull(sum(used), 0) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid = 255 and o.type = @type group by o.id) r
where #spt_space.id = r.id
--index page
update #spt_space
set indexp = r.used - data
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--unused page
update #spt_space
set unused = reserved - r.used
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id
--output
select #spt_space.name,
rows = convert(char(11), rows),
reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
' ' + 'KB'),
data = ltrim(str(data * d.low / 1024.,15,0) +
' ' + 'KB'),
index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
' ' + 'KB'),
unused = ltrim(str(unused * d.low / 1024.,15,0) +
' ' + 'KB')
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = 'E' order by len(ltrim(rtrim(reserved))) desc,reserved desc
go
----------------------------------------------------------------
--2、设置为系统存储过程
--sp_MS_marksystemobject 'sp_spaceused2'
--3、第三步,就可以查询了。
exec sp_spaceused2 'U',false,'要查询的表名'
go
--
--执行效果如下:
--name rows(这个就是每个表的行数)
--spt_values 2508 208 KB 96 KB 88 KB 24 KB
--MSreplication_options 3 16 KB 8 KB 8 KB 0 KB
-------------------------------------------------------
--以上测试在SQL2005以上版本中可以运行
展开全部
SELECT a.name, b.rows
FROM sysobjects a inner JOIN sysindexes b
ON b.id = a.id
WHERE a.xtype = 'U' AND b.indid IN (0,1)
U表示用户表,0和1过滤一些记录
详细请参考查询出来的结果。
如果仅要某些表,在WHERE后加上a.name in (表名1,表名2等)
FROM sysobjects a inner JOIN sysindexes b
ON b.id = a.id
WHERE a.xtype = 'U' AND b.indid IN (0,1)
U表示用户表,0和1过滤一些记录
详细请参考查询出来的结果。
如果仅要某些表,在WHERE后加上a.name in (表名1,表名2等)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-02-09
展开全部
select count(*)from [数据库名].[架构].[表名]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select count(0)from [数据库名]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select count(*) from Table
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询