我正在使用 interbase和firebird 的sql管理器,我有 130个表我想计算sql脚本中所有表的行。为什么我选择interbase的sql管理器和firebird是我在GDB格式的备份文件,我已经恢复了GDB文件,我需要计算行数。 我试过这个查询,但它无法正常工作
USE DatabaseName
GO
CREATE TABLE #temp (
table_name sysname ,
row_count INT,
reserved_size VARCHAR(50),
data_size VARCHAR(50),
index_size VARCHAR(50),
unused_size VARCHAR(50))
SET NOCOUNT ON
INSERT #temp
EXEC sp_msforeachtable 'sp_spaceused ''?'''
SELECT a.table_name,
a.row_count,
COUNT(*) AS col_count,
a.data_size
FROM #temp a
INNER JOIN information_schema.columns b
ON a.table_name collate database_default
= b.table_name collate database_default
GROUP BY a.table_name, a.row_count, a.data_size
ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) DESC
DROP TABLE #temp
它给出错误-104。
答案 0 :(得分:3)
要获取数据库中所有表的记录计数,您可以使用Firebird FAQ项"How to get record count for all tables?"中的此脚本。为了完整起见,我复制了下面的脚本:
set term !! ;
EXECUTE BLOCK
returns ( stm varchar(60), cnt integer )
as
BEGIN
for select cast('select count(*) from "'||trim(r.RDB$RELATION_NAME)||'"' as varchar(60))
from RDB$RELATIONS r
where (r.RDB$SYSTEM_FLAG is null or r.RDB$SYSTEM_FLAG = 0)
and r.RDB$VIEW_BLR is null
order by 1
into :stm
DO
BEGIN
execute statement :stm into :cnt;
suspend;
END
END
请注意,这仅计算当前交易可见的记录。这个脚本可以在Firebird中使用(我使用Firebird 2.5进行了测试),但可能不适用于Interbase,因为Firebird和Interbase方言在过去的13年中有所不同。
答案 1 :(得分:2)
不确定您对“sql manager”(可能是某些MS SQL Server工具?)的意思,但您发布的SQL与Firebird不兼容,是的。
Firebird具有特殊的gstat
(命令行)工具来分析数据库。 manual is available online。{{3}}。但是,AFAIK最接近它记录的数据是PK索引中的节点数。
可以创建从系统表读取表名的存储过程,并查询每个表的计数,但是值得这样做真的取决于你为什么需要记录计数。
答案 2 :(得分:0)
试试这个(带修复) -
SELECT
o.[object_id]
, [object_name] = s.name + '.' + o.name
, c.col_count
, r.row_count
, data_size = CAST(r.pages * 8 AS VARCHAR(10)) + ' KB'
FROM sys.objects o WITH (NOWAIT)
JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id]
JOIN (
SELECT c.[object_id], col_count = COUNT(1)
FROM sys.columns c WITH (NOWAIT)
GROUP BY c.[object_id]
) c ON c.[object_id] = o.[object_id]
JOIN (
SELECT
p.[object_id]
, row_count = SUM(p.[rows])
, pages = SUM(
CASE
WHEN a.[type] != 1 AND p.index_id < 2 THEN a.used_pages
WHEN p.index_id < 2 THEN a.data_pages ELSE 0
END
)
FROM sys.partitions p
JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
WHERE p.index_id IN (0, 1)
GROUP BY p.[object_id]
) r ON r.[object_id] = o.[object_id]
WHERE o.[type] = 'U'
ORDER BY r.pages DESC