首頁  >  文章  >  資料庫  >  SqlServer查询数据库所有用户表的记录数

SqlServer查询数据库所有用户表的记录数

WBOY
WBOY原創
2016-06-07 15:52:111546瀏覽

--创建临时表 create table #temp(Recordcount int ,tableName varchar(30)) --用游标将查询的记录数,插入临时表 declare @tablename varchar(30) declare @sql varchar(100) declare @str varchar(30) declare tablecursor cursor for select name from sy

--创建临时表

 

create table #temp(Recordcount int ,tableName varchar(30))

 

--用游标将查询的记录数,插入临时表


declare @tablename varchar(30)
declare @sql varchar(100)
declare @str varchar(30)
declare tablecursor cursor for
select name from sysobjects where xtype='u'
open tablecursor
fetch next from tablecursor into @tablename
while @@fetch_status=0
begin
set @str=@tablename
set @sql='insert into #temp(recordcount,tablename) select count(*),'+''''+@tablename+''''+' from 
'+@tablename
exec(@sql)
fetch next from tablecursor into @tablename
end
close tablecursor
deallocate tablecursor

 

--查询临时表,即可看见一个数据库的每个表的记录数

 

select * from #temp

 

 

--最后删除临时表


drop table #temp

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn