The code below starts from the oint that the table name and the field or column ( from all_columns_table) is hard-coded into the block. After I process flowed the anonymous block, I noticed that your code is not that far off.
How It Works:
1. I decided to count everything, number of records read(v_row_count), number of null fields(v_null_count) and the number of not null fields (v_field_count). This will serve as a check and also used to compute the percentages, if needed at some point. In order to get the total record count, I included in the SELECT statment the Primary Key field, it will always be present, whether your test column is null or not null. (See the Declaration Section);
2. Also in the Declaration Section, I declared the CURSOR, which IS absent the WHERE-Clause.
3. The Execution Section begins by Opening the declared CURSOR and LOOP thru each record passing values into the declared variable.
Test for the NULL , using the declared variable ,
v_null BOOLEAN := NULL, and incrementing the counters depending on the true or false comparison results. Here you may want to substitute your LENGTH <> 0 function. If the comparison test shows true, when to the naked-eye there is nothing, gsrbage could exist in the field if default spaces were not declared when field or entry was created.
4. When the LOOP is exhausted, all totals are printed. The CURSOR is closed and execution ends. Here you can calculate the the percentaqes.
5. The optional Exception Section is not included here.
==================== The Code ==================================
SET SERVEROUTPUT ON
DECLARE
v_row_count PLS_INTEGER := 0;
v_field_count PLS_INTEGER := 0;
v_null_count PLS_INTEGER := 0;
v_nullbit BOOLEAN := NULL;
v_test_column tblname.tblcolumn%TYPE;
-- Cursor Declaration
CURSOR tbl_cur IS
SELECT count(a.tblid),
a.tblcolumn
FROM tblname a;
-- Execution section
BEGIN
DBMS_OUTPUT.ENABLE(5);
-- Open cursor
OPEN tbl_cur;
-- Loop thru all records
LOOP
FETCH tbl_cur INTO v_row_count, v_test_column;
EXIT WHEN tbl_cur%NOTFOUND;
-- test for Nulls
IF v_test_column = v_nullbit
THEN v_null_count := v_null_count + 1;
ELSE v_field_count := v_field_count + 1
END IF;
END LOOP;
-- Display All Counts
DBMS_OUTPUT.PUT_LINE('Total Records: 'v_row_count
||','
|| 'Total Empty Cells: ' v_null_count
|| ','
|| 'Total Cells Used: ' v_field_count)
CLOSE tbl_cur;
END;
====================== end of code==============================
Suggestions: Work on getting the Execution block working then add selecting the test column and table name...
Warning: This code has not been tested....
Hope this helps. Email me if you have any questions....
|