我可以使用系统表获取行数,我还想包含一个以 MB 为单位显示每个表大小的字段。
我尝试了以下查询来获取表行数。
declare @kernel_mode varchar(15)
if @@trancount = 0
begin
set chained off
end
set transaction isolation level 1
select @kernel_mode = @@kernelmode
select
ob.name as Table_Name,
ob.type as Type,
ta.rowcnt as Row_Count,
ob.crdate as Created_Date,
ta.statmoddate as Last_Modified_Date
into #t
from sysobjects ob, systabstats ta
where ob.type="U"
and ta.id=ob.id
order by ob.name
if (@kernel_mode = 'process')
begin
exec sp_autoformat @fulltabname = #t,
@selectlist = "Table_Name, Type, Row_Count, Created_Date, Last_Modified_Date",
@orderby = "order by Table_Name asc"
end
else
begin
exec sp_autoformat @fulltabname = #t,
@selectlist = "Table_Name, Type, Row_Count, Created_Date, Last_Modified_Date",
@orderby = "order by Table_Name asc"
end
drop table #t
根据@markp-fuso的建议,我提出了以下查询。
declare @kernel_mode varchar(15)
if @@trancount = 0
begin
set chained off
end
set transaction isolation level 1
select @kernel_mode = @@kernelmode
select
ob.name as Table_Name,
ob.type as Type,
ta.rowcnt as Row_Count,
(cast(ta.pagecnt as
bigint)*@@maxpagesize/1024.0/1024.0 as
Table_Size_in_MB
ob.crdate as Created_Date,
ta.statmoddate as Last_Modified_Date
into #t
from sysobjects ob, systabstats ta
where ob.type="U"
and ta.id=ob.id
order by ob.name
if (@kernel_mode = 'process')
begin
exec sp_autoformat @fulltabname = #t,
@selectlist = "Table_Name, Type,
Row_Count, Created_Date,
Last_Modified_Date",
@orderby = "order by Table_Name asc"
end
else
begin
exec sp_autoformat @fulltabname = #t,
@selectlist = "Table_Name, Type,
Row_Count, Created_Date,
Last_Modified_Date",
@orderby = "order by Table_Name asc"
end