Find Unused Columns in Oracle Database
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;
No comments:
Post a Comment