   IS
   BEGIN
     RETURN
-        chr(10)||'  -- delete '||m_tab_name||'(s) not being referenced any more'
-      ||chr(10)||'  IF '||m_tab_name||'_ref_id IS NOT NULL THEN'
+        chr(10)||'  -- delete '||lower(m_tab_name)||'(s) not being referenced any more'
+      ||chr(10)||'  IF '||lower(m_tab_name)||'_ref_id IS NOT NULL THEN'
       ||chr(10)||'    DELETE FROM'
-      ||chr(10)||'      '||m_tab_name||' m'
+      ||chr(10)||'      '||lower(m_tab_name)||' m'
       ||chr(10)||'    WHERE EXISTS ('
       ||chr(10)||'      SELECT'
       ||chr(10)||'        1'
       ||chr(10)||'      FROM'
-      ||chr(10)||'        TABLE('||m_tab_name||'_ref_id) a'
+      ||chr(10)||'        TABLE('||lower(m_tab_name)||'_ref_id) a'
       ||chr(10)||'      LEFT JOIN'
-      ||chr(10)||'        '||n_m_tab_name||' n2m'
-      ||chr(10)||'        ON n2m.'||fk_m_column_name||' = a.COLUMN_NAME'
+      ||chr(10)||'        '||lower(n_m_tab_name)||' n2m'
+      ||chr(10)||'        ON n2m.'||lower(fk_m_column_name)||' = a.COLUMN_VALUE'
       ||chr(10)||'      WHERE'
       ||chr(10)||'        a.COLUMN_VALUE = m.id'
-      ||chr(10)||'        n2m.'||fk_m_column_name||' IS NULL'
+      ||chr(10)||'        n2m.'||lower(fk_m_column_name)||' IS NULL'
       ||chr(10)||'    );'
       ||chr(10)||'  END IF;'
       ||chr(10);

        all_cons_columns a
        ON a.constraint_name = c.constraint_name
       AND a.table_name = c.table_name
      JOIN
        all_constraints c2
        ON c2.constraint_name = c.r_constraint_name

          ON pk.table_name = fk.table_name
         AND pk.owner = fk.owner
         AND pk.column_name = fka.column_name
        WHERE
          fk.table_name = c.table_name
          AND fk.owner = c.owner
          AND fk.constraint_type = 'R'
          AND fk.delete_rule = 'NO ACTION'
          AND pk.position = 1
      ) p
      -- get tables from n:m relationships to cleanup
      -- the FK has to be delete_rule = 'CASCADE' to decide for cleanup
