Select column names whose entries are not null

前端 未结 2 491
心在旅途
心在旅途 2020-12-21 23:43

I would like to have a list of those columns of a table that have at least one non-NULL data entries in them.

In other words, I would like to get the co

2条回答
  •  有刺的猬
    2020-12-21 23:53

    Use this procedure this will print columns names of a table which have atleast one not null rows.

    create or replace procedure list_col_notNull(tblName in varchar2)
    as
    lv_col_name varchar2(200);
    lv_ctr number;
    lv_sql varchar2(400);
    CURSOR cur_col_name is
    SELECT column_name
    FROM USER_TAB_COLUMNS U
    WHERE table_name = tblName order by column_name asc;
    begin
    open cur_col_name;
    
    LOOP
        FETCH cur_col_name INTO lv_col_name; 
        EXIT WHEN cur_col_name%NOTFOUND; 
        lv_sql := 'select count(1) From ' || tblName || ' where ' || lv_col_name || ' is not null'  ;  
        EXECUTE IMMEDIATE lv_sql into lv_ctr;
        if lv_ctr > 0
        then
            dbms_output.put_line(lv_col_name);
        end if;
    

提交回复
热议问题