SQLSERVER中統計所有表的記錄數

2021-06-29 16:34:02 字數 3359 閱讀 9291

sqlserver中統計所有表的記錄數

2009-06-25 17:07:36

分類: linux

曾經試著寫過乙個儲存過程,作用就是刪除所有表的內容,思路是這樣的:

首先通過sysobjects表構造乙個sql語句字串'delete 表名',其中表名就是sysobjects中的name列,把這些delete語句字串連線起來的方法一是通過游標,二則是直接利用如下語句:

select @sql = @sql + 'delete ' + name from sysobjects where xtype='u';

這是乙個很有用的技巧,在合適的地方用會很大程度的優化語句執行速度.

然後就是通過exec(@sql)執行該字串.

而把資料庫所有表的記錄數統計出來和這個思路幾乎完全一樣,不同的就是把'delete 表名' 改為'select 表名,count(1) from 表名',主要這點不同而已,如果構造完字串並執行完畢,可以把結果輸出到乙個臨時表,那麼再統計所有記錄數就輕而易舉了.

下面就是我寫的乙個語句:

declare @sql varchar(8000),@count int,@step int

set nocount on

--@step

越大執行速度越快,但如果太大會造成生成的sql字串超出限制導致語句不完整出錯

--建議為50

set @step = 50

if object_id(n'tempdb.db.#temp') is not null

drop table #temp

create table #temp (name sysname,count numeric(18))

if object_id(n'tempdb.db.#temp1') is not null

drop table #temp1

create table #temp1 (id int identity(1,1),name sysname)

insert into #temp1(name)

select name from sysobjects where xtype = 'u';

set @count = @@rowcount while @count>0

begin

set @sql = ''

select @sql = @sql + ' select ''' + name + ''',count(1) from ' + name + ' union'

from #temp1 where id > @count - @step and id <= @count

set @sql = left(@sql,len(@sql) - len('union'))

insert into #temp exec (@sql)

set @count = @count - @step

endselect count(count) 總表數,sum(count) 總記錄數 from #temp

select * from #temp order by count,name

set nocount off

經過測試,該方法可以通過,不過有時候@step的值需要手動設定一下,@step=50應該就可以滿足大部分資料庫的需要了.如果表名都比較短的話,可以設定@step=80或者100.

後來我又去上網搜尋其他統計資料庫所有表記錄數的語句,發現了下面的方法:create table #(id int identity ,tblname varchar(50),num int)

declare @name varchar(30)

declare roy cursor for select name from sysobjects where xtype='u'

open roy

fetch next from roy into @name

while @@fetch_status=0

begin

declare @i int

declare @sql nvarchar(1000)

set @sql='select @n=count(1) from '+@name

exec sp_executesql @sql,n'@n int output',@i output

insert into # select @name,@i

fetch next from roy into @name

endclose roy

deallocate roy

select * from #

該方法用到了游標,如果資料庫表很多的話速度可能會比較慢,但是該錶不受表名長短影響,對所有資料庫都適用.

第三種方法,利用系統的物件表和索引表:

set nocount on

if object_id(n'tempdb.db.#temp') is not null

drop table #temp

create table #temp (name sysname,count numeric(18))

insert into #temp

select o.name,i.rows

from sysobjects o,sysindexes i 

where o.id=i.id and o.xtype='u' and i.indid<2

select count(count) 總表數,sum(count) 總記錄數 from #temp

select * from #temp

set nocount off

該方法執行速度絕對最快,但是結果好象並不是太準確,稍微有一些偏差.所以如果對資料量比較大而且對統計結果要求比較低的,該方法絕對是第一選擇.如果要求統計絕對準確的記錄數而且表的數量比較多的話,個人感覺第乙個方法應該是個不錯的選擇.

第三個方法主要是利用了系統索引表sysindexes中索引id indid<1的行中的rows列存有該錶的行數這一特點.

最後一種方法是利用隱藏未公開的系統儲存過程sp_msforeachtable

create table #temp (tablename varchar (255), rowcnt int)

exec sp_msforeachtable 'insert into #temp select ''?'', count(*) from ?'

select tablename, rowcnt from #temp order by tablename

drop table #temp

從mssql6.5開始,微軟提供了兩個不公開,非常有用的系統儲存過程sp_msforeachtable和sp_msforeachdb,用於遍歷某個資料庫的每個表和遍歷dbms管理下的每個資料庫。-

所有的表sqlserver

select o.name as tablename,user name o.uid as owner,isnull ptb.value,n as tabledescription,c.colid as fieldid,c.name as fieldname,quotename t.name cas...

Sql Server 刪除所有表

如果由於外來鍵約束刪除table失敗,則先刪除所有約束 第1步 刪除所有表的外來鍵約束 declarec1cursorfor select alter table object name parent obj drop constraint name fromsysobjects wherextyp...

SQLserver查詢所有表和表下面所有列

select case when a.colorder 1 then d.name else null end 表名,a.colorder 字段序號,a.name 欄位名,case when columnproperty a.id,a.name,isidentity 1 then else end ...