简体   繁体   English

如何还从以下查询中选择行数? - 甲骨文 SQL

[英]How to also select row count from following query? - Oracle SQL

Need to also select the row/record count using the following query in Oracle SQL.还需要在 Oracle SQL 中使用以下查询选择行/记录计数。 How would I go about doing this?我该怎么做呢?

SELECT DISTINCT t.OWNER AS TABLE_SCHEMA, t.Table_Name, c.COLUMN_NAME, c.DATA_TYPE, 
                ( 
                    SELECT CASE WHEN cons.CONSTRAINT_TYPE = 'P' THEN 'Primary Key' ELSE NULL END 
                    FROM SYS.all_cons_columns cols 
                        INNER JOIN SYS.all_constraints cons ON cons.constraint_name=cols.constraint_name AND cons.constraint_type = 'P' 
                    WHERE cols.Column_name=c.column_name AND cols.TABLE_NAME=c.TABLE_NAME AND cols.OWNER=c.OWNER 
                ) AS CONSTRAINT_TYPE, c.DATA_PRECISION, c.DATA_SCALE 
                FROM SYS.ALL_TABLES t 
                    INNER JOIN SYS.all_tab_columns c ON c.TABLE_NAME=t.TABLE_NAME AND c.OWNER=t.OWNER
                WHERE t.OWNER = 'MY_SCHEMA_NAME' AND t.Table_Name = 'MY_TABLE_NAME'

If you can live with an estimate number of rows (whose accuracy depends on the freshness of each table's statistics), you can just use column NUM_ROWS from ALL_TABLES :如果您可以接受估计的行数(其准确性取决于每个表统计信息的新鲜度),您可以只使用ALL_TABLES NUM_ROWS列:

SELECT 
    t.OWNER AS TABLE_SCHEMA, 
    t.Table_Name, 
    c.COLUMN_NAME, 
    c.DATA_TYPE, 
    t.NUM_ROWS, --> here
    ...
FROM all_tables t
INNER JOIN all_tab_columns c ...

Note that DISTINCT seem superfluous here - your query generates one record per column in the table.请注意, DISTINCT在这里似乎是多余的 - 您的查询为表中的每列生成一条记录。

If you really need the exact count, then you can use an inline query - but you need to hardcode the table name:如果您确实需要确切的计数,那么您可以使用内联查询 - 但您需要对表名进行硬编码:

SELECT 
    t.OWNER AS TABLE_SCHEMA, 
    t.Table_Name, 
    c.COLUMN_NAME, 
    c.DATA_TYPE, 
    (SELECT COUNT(*) FROM my_schema_name.my_table_name) no_rows,
    ...
FROM all_tables t
INNER JOIN all_tab_columns c ...
WHERE t.OWNER = 'MY_SCHEMA_NAME' AND t.Table_Name = 'MY_TABLE_NAME'

Not sure which SQL you are using but you might put the whole result into a temp table like不确定您使用的是哪个 SQL,但您可能会将整个结果放入一个临时表中,例如

INTO temp temp_table_count WITH NO LOG;

SELECT COUNT(*) FROM temp_table_count;

https://www.oracletutorial.com/oracle-basics/oracle-private-temporary-table/ the reference https://www.oracletutorial.com/oracle-basics/oracle-private-temporary-table/参考

INSERT INTO ora$ppt_temp_table_count(ADD ALL COLUMNS HERE 
COMMASEPARATED)
SELECT col1, col2, col3
FROM source_table
WHERE condition; <-- your query

SELECT COUNT(*) FROM ora$ppt_temp_table_count;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM