如何从以下 DB2 查询中检索 row/record 计数?

How to also retrieve row/record count from following DB2 query?

还需要 select 给定以下查询的总记录数。我该怎么做?


SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME, 
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE 
    FROM SYSIBM.SYSTABLES t 
        INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
    WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'

任何帮助将不胜感激,谢谢

Need to also select the total record count given the following query.

SELECT DISTINCT 似乎是多余的。系统 tables 不应有重复项。如果您得到重复项,可能是因为 JOIN 条件不完整——您应该同时加入 table 名称和创建者。

所以只需使用COUNT(*)

SELECT COUNT(*) 
FROM SYSIBM.SYSTABLES t INNER JOIN
     SYSIBM.SYSCOLUMNS c
     ON c.TBNAME = t.Name AND
        c.TBCREATOR = t.TBCREATOR
WHERE t.Creator = 'MY_SCHEMA_NAME' AND
      t.Name = 'MY_TABLE_NAME';

对于任何 SELECT 声明,包括您的声明:

SELECT T.*, COUNT(1) OVER() AS ROW_COUNT
FROM
(
SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME, 
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE 
    FROM SYSIBM.SYSTABLES t 
        INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
    WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'
) T;