mssql里求数据库中所有表的记录数

samool 发布暂无评论8740 阅读

AI摘要

本文介绍了一个SQL Server的脚本,该脚本通过创建临时表和游标,遍历数据库中所有用户表,计算并存储每个表的记录数,最后输出结果。

create table #temp(name varchar(50),count int)
declare @tablename varchar(50),@sql varchar(500)
declare gettablenum_cur cursor for select name from sysobjects where xtype='u'
open gettablenum_cur
fetch gettablenum_cur into @tablename
while @@fetch_status=0
begin
select @sql='insert #temp(name,count) '+'select '+''''+@tablename+''''+','+'count(1) from '+@tablename
exec(@sql)
fetch gettablenum_cur into @tablename
end
select * from #temp order by name
close gettablenum_cur
deallocate gettablenum_cur
drop table #temp

  1. 01SaMDeditor V2.4 更新内容2026-12-02
  2. 02恭喜《轻武爱学习》小程序备案成功!2026-11-24
  3. 03SaView 图阅 1.1.1 更新日志2026-11-18
  4. 04Book2EPUB 1.0.4 更新日志2026-11-18
  5. 05SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  6. 06免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  7. 07SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
  8. 08PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
  9. 09LiteLLM v1.106.0 更新内容总结2026-10-20
  10. 10AI Native 受控软件工程2026-10-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言