Sometimes during development of new systems, you may add new columns to tables and then you don't use it and forget dropping it.
So you want to know which these columns to drop.
Usually unused columns have NULL value, So I created a function to return array of column names in my schema have NULL value.
I created GET_NULL_COLUMNS function returns VARRAY of varchar2
It has only one parameter (IN_TABLE_NAME)
If I pass a value for IN_TABLE_NAME then it will return NULL columns in this table only, otherwise it will return NULL columns in entire schema.
CREATE OR REPLACE FUNCTION GET_NULL_COLUMNS (
IN_TABLE_NAME VARCHAR2 DEFAULT NULL)
RETURN SYS.ODCIVARCHAR2LIST
IS
L_NULL_COLS SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST ();
LN$ROW_COUNT NUMBER;
LN$NULL_COUNT NUMBER;
LN$INDEX NUMBER := 1;
CURSOR LCUR$COLS (P_TABLE_NAME VARCHAR2)
IS
SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLS
WHERE DECODE (P_TABLE_NAME, TABLE_NAME, 1, NULL, 1, 0) = 1;
BEGIN
FOR LREC$COLS IN LCUR$COLS (IN_TABLE_NAME)
LOOP
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM ' || LREC$COLS.TABLE_NAME
INTO LN$ROW_COUNT;
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM '
|| LREC$COLS.TABLE_NAME
|| ' WHERE '
|| LREC$COLS.COLUMN_NAME
|| ' IS NULL'
INTO LN$NULL_COUNT;
IF LN$ROW_COUNT = LN$NULL_COUNT
THEN
L_NULL_COLS.EXTEND;
L_NULL_COLS (LN$INDEX) :=
LREC$COLS.TABLE_NAME || '.' || LREC$COLS.COLUMN_NAME;
LN$INDEX := LN$INDEX + 1;
END IF;
END LOOP;
RETURN L_NULL_COLS;
END;
Let's now test this function in SCOTT schema
SELECT COLUMN_VALUE FROM TABLE (GET_NULL_COLUMNS);
The output of query is
Thanks
Intersting concept and shows the use of some nice features like ODCI. But is a large system could be counting a lot of rows when the answer is already know; the NULLABLE flag tells us this is a column to look at or ignore.
ReplyDeleteSee the nullable = 'Y' in codw
Also no need to count a table for every column.
See the added IF statement in the loop
I also stripped the dollar sign, a build in refactor of my workflow.
[code]
CREATE OR REPLACE FUNCTION get_null_columns ( in_table_name VARCHAR2 DEFAULT NULL)
RETURN sys.odcivarchar2list
IS
l_null_cols sys.odcivarchar2list := sys.odcivarchar2list ();
lnRowCount NUMBER;
lnNullCount NUMBER;
lnIndex NUMBER := 1;
lCurrTab VARCHAR2( 32 ) := '-None-' ;
CURSOR lCurCols (p_table_name VARCHAR2)
IS
SELECT table_name,
column_name
FROM user_tab_cols
WHERE nullable = 'Y'
AND DECODE (p_table_name,
table_name, 1,
NULL, 1,
0) = 1 ;
BEGIN
FOR lRecCols IN lCurCols (in_table_name)
LOOP
IF lCurrTab <> lRecCols.table_name THEN
lCurrTab := lRecCols.table_name ;
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM '
|| lRecCols.table_name INTO lnRowCount;
END IF;
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM '
|| lRecCols.table_name
|| ' WHERE '
|| lRecCols.column_name
|| ' IS NULL' INTO lnNullCount;
IF lnRowCount = lnNullCount THEN
l_null_cols.extend;
l_null_cols (lnIndex) := lRecCols.table_name
|| '.'
|| lRecCols.column_name;
lnIndex := lnIndex + 1;
END IF;
END LOOP;
RETURN l_null_cols;
END;
/
[/code]
As I see it, there are things done in the development environment that should be designed. We never, yes, it is true, add columns and forget to drop them.
ReplyDelete