ORACLE queries the data volume and empty data volume of all fields in all tables under the database, the empty rate and the maximum update time and the minimum update time

First create a table, which contains all the tables under the library, and time update fields.

The purpose of this table is to traverse the table and time fields, and also to identify the meaning of the table.

 

Then create another table, the fields in the table are the table name, field name, field data is empty, the total amount of data in the field, field empty rate and minimum update time maximum update time.

The next step is the stored procedure.

CREATE OR REPLACE PROCEDURE "TABLEFIELD" 
AS
        V_SQL VARCHAR2(2000);
        V_FIELD VARCHAR2(200);
        V_NUM INT:='0';
        V_T_NUM INT:='0';
        V_TABLENAME VARCHAR2(200);
        V_COUNT INT:='0';
        V_T_COUNT INT:='6';
        V_BUSTIMEFIELD    VARCHAR2(200);
     


    --Remove the     table name and time field from the table and insert it into the cursor            CURSOR TAB_CURSOR IS 
    SELECT TABLENAME, BUSTIMEFILED FROM DIC_TABLE ORDER BY TABLENAME; --Remove  
    
    the table name and all fields from the system table and insert them into the cursor
    CURSOR EMP_CURSOR( V_TABLENAME ALL_TAB_COLUMNS.TABLE_NAME%TYPE) IS 
                        SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS  
                        WHERE TABLE_NAME=V_TABLENAME;
        
 - Define a variable
TAB TAB_CURSOR%ROWTYPE;
EMPROW EMP_CURSOR%TYPE;

BEGIN

-First delete the data in the table    

DELETE FROM DIC_TABLEINFO; --Open the
  cursor to start the loop. Because there are two cursors, one is the cursor of the table name, and the other is the cursor of the field, so we need to nest the cursor to loop.
  OPEN TAB_CURSOR;
  LOOP
  FETCH TAB_CURSOR INTO TAB;
  EXIT WHEN TAB_CURSOR%NOTFOUND;    
        
                        
                        BEGIN
                        OPEN EMP_CURSOR(TAB.TABLENAME); 
                        LOOP
                        FETCH EMP_CURSOR INTO EMP;
                        EXIT WHEN EMP_CURSOR%NOTFOUND;    
                        
                        

                                DBMS_OUTPUT.PUT_LINE(EMP.COLUMN_NAME);
                                --插入字段名称
                                V_SQL :='INSERT INTO DIC_TABLEINFO(CLONAME) VALUES('''||EMP.COLUMN_NAME||''')'; 
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT;    
                                
                                --更新字段中为NULL的数据量
                                V_SQL :='UPDATE DIC_TABLEINFO SET COLCOUNT=
                                ( SELECT COUNT(*) AS NUM FROM  '||TAB.TABLENAME||' WHERE '||EMP.COLUMN_NAME||' IS  NULL)
                                WHERE CLONAME='''||EMP.COLUMN_NAME||''' AND TABLE_NAME IS NULL';
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT;    


                                --Update                                 the total number of fields in the table-- DBMS_OUTPUT.PUT_LINE(V_SQL);
                                V_SQL :='UPDATE DIC_TABLEINFO SET TOTALCOUNT=(SELECT COUNT(*) FROM'||TAB.TABLENAME||') WHERE TABLE_NAME IS NULL';
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT;     --Update
                                
                                to NULL percentage (retain four decimals), when the total amount of data is zero, directly set the percentage to 0
                                UPDATE DIC_TABLEINFO SET PERSENT=CASE
                                WHEN CAST(TOTALCOUNT AS FLOAT)=0 THEN 0
                                ELSE (ROUND(CAST(COLCOUNT AS FLOAT)/CAST(TOTALCOUNT AS FLOAT),2))
                                END;
                                
                                
                        END LOOP;   
                        CLOSE EMP_CURSOR;         
                        END;


                                --Update                                 the table name in the data V_SQL :='UPDATE DIC_TABLEINFO SET TABLE_NAME ='''||TAB.TABLENAME||''' WHERE TABLE_NAME IS NULL';
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT; 
                            

                                --更新最大业务时间
                                V_SQL :='UPDATE DIC_TABLEINFO SET  MAXDATE=(SELECT MAX('||TAB.BUSTIMEFILED||')  FROM '||TAB.TABLENAME||') WHERE TABLE_NAME='''||TAB.TABLENAME||'''';
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT;
                                --更新最小业务时间
                                V_SQL :='UPDATE DIC_TABLEINFO SET  MINDATE=(SELECT MIN('||TAB.BUSTIMEFILED||')  FROM '||TAB.TABLENAME||') WHERE TABLE_NAME='''||TAB.TABLENAME||'''';
                                EXECUTE IMMEDIATE V_SQL;
                                COMMIT;                                   
                END LOOP;   
                CLOSE TAB_CURSOR;         
                END;

 

So far, call the following outside to get the desired fields in the table.

 

Guess you like

Origin blog.csdn.net/qq_37823979/article/details/97938528