0

I have been stuck into a question. The question is I want to get all Table name with their Row Count from Teradata.

I have this query which gives me all View Name from a specific Schema.

I ] SELECT TableName FROM dbc.tables WHERE tablekind='V' AND databasename='SCHEMA' order by TableName;

& I have this query which gives me row count for a specific Table/View in Schema.

II ] SELECT COUNT(*) as RowsNum FROM SCHEMA.TABLE_NAME;

Now can anyone tell me what to do to get the result from Query I (TableName) and put it into QUERY II (TABLE_NAME) You help will be appreciated.

Thanks in advance,

Vrinda

4

3 回答 3

1

使用 dnoeth 的答案,而是使用 create “create volatile table”,这将使用您的假脱机来创建表,并在会话关闭时删除所有数据。您无需写入权限即可使用易失性表。

于 2013-08-12T23:24:21.053 回答
1

这是一个从数据库中的所有表中收集行数的 SP,它非常基本,没有错误检查等。

它使用 dbc.SysExecSQL 或 EXECUTE IMMEDIATE 显示游标和动态 SQL:

CREATE SET TABLE RowCounts
     (
      DatabaseName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
      TableName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
      RowCount BIGINT,
      COllectTimeStamp TIMESTAMP(2))
PRIMARY INDEX ( DatabaseName ,TableName )
;

REPLACE PROCEDURE GetRowCounts(IN DBName VARCHAR(30))
BEGIN
  DECLARE SqlTxt VARCHAR(500);
   FOR cur AS
      SELECT
         TRIM(DatabaseName) AS DBName,
         TRIM(TableName) AS TabName
      FROM dbc.Tables
      WHERE DatabaseName = :DBName
      AND TableKind = 'T'
   DO
     SET SqlTxt =
         'INSERT INTO RowCounts ' ||
         'SELECT ' ||
         '''' || cur.DBName || '''' || ',' ||
         '''' || cur.TabName || '''' || ',' ||
         'CAST(COUNT(*) AS BIGINT)' ||  ',' ||
         'CURRENT_TIMESTAMP(2) ' ||
         'FROM ' || cur.DBName ||
         '.' || cur.TabName || ';';

     --CALL dbc.sysexecsql(:SqlTxt);
     EXECUTE IMMEDIATE sqlTxt;
   END FOR;
END;

如果您无法创建表或 SP,您可以使用 VOLATILE TABLE(如 DrBailey 建议的那样)并运行以下查询返回的 INSERT:

SELECT
   'INSERT INTO RowCounts ' ||
   'SELECT ' ||
   '''' || DatabaseName || '''' || ',' ||
   '''' || TableName || '''' || ',' ||
   'CAST(COUNT(*) AS BIGINT)' ||  ',' ||
   'CURRENT_TIMESTAMP(2) ' ||
   'FROM ' || DatabaseName ||
   '.' || TableName || ';'
FROM dbc.tablesV 
WHERE tablekind='V' 
AND databasename='schema' 
ORDER BY TableName;

但是您的系统上可能已经存在这样的例程,您可能会问 DBA。如果它不必是 100% 准确的,也可以从收集的统计数据中提取此信息。

于 2013-08-08T22:15:02.047 回答
0

有什么方法可以在不使用 count( ) 的情况下从系统表中找到表/视图的行数。Count( ) 需要很长时间才能运行大表。dbc.statsv 视图仅包含表,因为我们无法收集视图的统计信息。

于 2019-08-07T10:30:40.017 回答