【转】SQL Server 查询表的记录数(3种方法,推荐第一种)
2015-12-28 13:49
429 查看
--SQL Server 查询表的记录数 --one: 使用系统表. SELECT object_name (i.id) TableName, rows as RowCnt FROM sysindexes i INNER JOIN sysObjects o ON (o.id = i.id AND o.xType = 'U ') WHERE indid < 2 ORDER BY TableName --****************** --two: 使用未公开的过程 "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 --****************** -- three: 使用游标.cursor SET NOCOUNT ON DECLARE @tableName VARCHAR (255), @sql VARCHAR (300) CREATE TABLE #temp (TableName VARCHAR (255), rowCnt INT) DECLARE myCursor CURSOR FAST_FORWARD READ_ONLY FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'base table ' OPEN myCursor FETCH NEXT FROM myCursor INTO @tableName WHILE @@FETCH_STATUS = 0 BEGIN EXEC ( 'INSERT INTO #temp (TableName, rowCnt) SELECT ''' + @tableName + ''' as tableName, count(*) as rowCnt from ' + @tableName) FETCH NEXT FROM myCursor INTO @tableName END SELECT TableName, RowCnt FROM #temp ORDER BY TableName CLOSE myCursor DEALLOCATE myCursor DROP TABLE #temp
相关文章推荐
- 6.zabbix学习笔记:zabbix监控mysql(一)
- MySQL中函数CONCAT及GROUP_CONCAT
- java中oracle语句的时间表示
- Linux下,强制删除oracle10g安装文件后再重装
- MySQL字符集乱码简单讲解
- 查看SQL-Server数据库所有触发器信息
- 更改mysql数据库目录
- Java使用Pipeline对Redis批量读写(hmset&hgetall)
- ORACLE转义字符的那点事
- 看了几天的redis稍微总结一下数据类型及命令(字符型)
- MYSQL备份(直接拷贝数据库文件)
- oracle 删除用户,表空间;循环删除表
- MYSQL5.7.10 安装文档
- Java 失宠于 Oracle,未来会怎样?
- mysql中FIND_IN_SET的使用方法
- Icingaweb2监控oracle数据库的安装配置流程
- Icingaweb2监控oracle数据库的安装配置流程
- SQL server2012 1433端口不通解决方法
- Mac下安装新版MySQL5.7
- Mac下MySQL卸载方法