這適用於T-SQL。不知道這是否會在Sybase工作...
CREATE TABLE #RecCounts (TableName varchar(100), RecCount int)
SELECT object_id INTO #Processed FROM sys.tables WHERE name = '(no such table)'
DECLARE @TableId int, @TableName varchar(255), @TableSchema varchar(100), @CountSQL varchar(255)
SELECT @TableId = MIN(object_id) FROM sys.tables WHERE type = 'U'
AND object_id NOT IN (SELECT object_id FROM #Processed)
SET @TableId = ISNULL(@TableId, -1)
WHILE @TableId > -1 BEGIN
PRINT @TableId
SELECT @TableName = name FROM sys.tables WHERE type = 'U' AND object_id = @TableId
SELECT @TableSchema = s.name, @TableName = t.name
FROM sys.Tables t
INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
WHERE t.object_id = @TableId
SET @CountSQL = 'DECLARE @RecCount int
SELECT @RecCount = COUNT(*) FROM ' + @TableSchema + '.' + @TableName + '
INSERT INTO #RecCounts (TableName, RecCount) VALUES (''' + @TableName + ''', @RecCount)'
PRINT @CountSQL
EXEC(@CountSQL)
INSERT INTO #Processed (object_id) VALUES(@TableId)
SELECT @TableId = MIN(object_id) FROM sys.tables WHERE type = 'U'
AND object_id NOT IN (SELECT object_id FROM #Processed)
END
SELECT * FROM #RecCounts
* <勇於提起「D字」> * .......你有沒有想過使用動態SQL呢? – Jeroen
閱讀本文後:[如何獲取sybase IQ中所有表的記錄數](http://scn.sap.com/thread/3448070) –
在SQL Server中,記錄數存儲在索引元數據中表。您可能會發現這有幫助http://stackoverflow.com/questions/428458/counting-rows-for-all-tables-at-once。 –