比如一个表,有50个字段。某一条记录,其中的13个字段有值。我怎么把这个数量给找出来?

解决方案 »

  1.   

    select count(*)
    from user_tab_columns
    where table_name='XXX'
    and num_nulls=0;
      

  2.   

    那就用个过程来一个字段一个字段地找,用SQL不好写、。
      

  3.   

    写sql也行..就是写50个decode(字段,null,0,1)然后把这50个加起来..
      

  4.   

    要是就看录入了多少个的话很简单..
    比如说这是数据
     a b c d
     1 0 1 0
     1 1 1 1
    第一行录入了两个
    第二行录入了四个
    是要这个效果不?
    就定义个存储过程.
    然后动态拼接sql也就是说从user_tab_columns中获取某个表的所有字段比如说是
    TABLE1吧.
    select column_name
      from user_tab_columns
     where table_name = 'TABLE1'
     order by column_id;
    然后循环动态拼接sql
    用decode然后把这些都加起来.
    循环的时候用dbms_output.putline打出来..
    这样的话就能看到每条记录都输入了多少个字段.
      

  5.   

    我在本机有个table1表就拿我这个来说吧.就当是你有table1表
    declare
      tblName varchar2(31) := 'table1';
      strSql  varchar2(4000);
      colName varchar2(31);
      cursor getCols is
        select column_name
          from user_tab_columns
         where table_name = upper(tblName)
         order by column_id;
      type refCur is ref cursor;
      refCount refCur;
      intCount number;
    begin
      strSql := 'select ';
      open getCols;
      loop
        fetch getCols
          into colName;
        exit when getCols%notfound;
        strSql := strSql || 'decode(' || colName || ',null,0,1)+';
      end loop;
      close getCols;
      strSql := substr(strSql, 1, length(strSql) - 1);
      strSql := strSql || ' count from ' || tblName;
      open refCount for strSql;
      loop
        fetch refCount
          into intCount;
        exit when refCount%notfound;
        dbms_output.put_line(intCount);
      end loop;
      close refCount;
    end;
    将tblName变量里的表名改成你自己的表名就能看到结果了..
      

  6.   

    select decode(fld1,null,0,1)+decode(fld2,null,0,1)+....+decode(fld50,null,0,1) from yourtable得到的结果就是你有值的字段数。
    家里没有安装oracle,你试试看。
      

  7.   

    晕,没看到你有200个子段,要用动态SQL了
      

  8.   

    看来只能用动态SQL了。谢谢各位的帮助。