Oracle SQL检索表中所有列的空白值,空值,非空值,不同值

时间:2019-04-13 09:26:47

标签: sql oracle ora-00907

我从上一篇文章中选择了此代码,并尝试调整sql以计算该列的记录空白计数。.但是我遇到以下错误..我无法解决该错误..请您帮助

select owner, table_name, column_name,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(distinct "' || column_name || '") as c '
|| 'from "' || owner || '"."' || table_name || '"'))
returning content)) as distinct_count,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(case when (' || column_name || ' = ' ' ) then 0 end) as c '
|| 'from "' || owner || '"."' || table_name || '"'))
returning content)) as null_count,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(case when "' || column_name || '" is not null then 1 end) as c '
|| 'from "' || owner || '"."' || table_name || '"'))
returning content)) as notnull_count
from all_tab_columns
where owner = 'JAMES'
and table_name = 'TEST'
and data_type in ('NUMBER', 'DATE', 'TIMESTAMP', 'CHAR', 'VARCHAR2',
'NCHAR', 'NVARCHAR2');

ORA-00907:缺少右括号 00907. 00000-“缺少右括号” *原因:
*行动: 在第9行的错误:第58列

2 个答案:

答案 0 :(得分:0)

尝试一下:

select owner, table_name, column_name,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(distinct "' || column_name || '") as c '
|| 'from "' || owner || '"."' || table_name || '"'))
returning content)) as distinct_count,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(case when (''' || column_name || ''' = '' ) then 0 end) as c from ' || owner || '''.''' || table_name || ''''))
returning content)) as null_count,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(case when "' || column_name || '" is not null then 1 end) as c '
|| 'from "' || owner || '"."' || table_name || '"'))
returning content)) as notnull_count
from all_tab_columns
where owner = 'JAMES'
and table_name = 'TEST'
and data_type in ('NUMBER', 'DATE', 'TIMESTAMP', 'CHAR', 'VARCHAR2',
'NCHAR', 'NVARCHAR2');

答案 1 :(得分:0)

似乎不需要长度为null的字符串( '' ),并且在带引号的字符串中每个字符串都提供一个以上的引号。因此,您需要在行:9列:58 上将字符串 '' 转换为 '''' 在您的代码中

select owner,
       table_name,
       column_name,
       to_number(xmlquery('/ROWSET/ROW/C/text()' passing
                          xmltype(dbms_xmlgen.getxml('select count(distinct "' ||
                                                     column_name ||
                                                     '") as c ' || 'from "' ||
                                                     owner || '"."' ||
                                                     table_name || '"'))
                          returning content)) as distinct_count,
       to_number(xmlquery('/ROWSET/ROW/C/text()' passing
                          xmltype(dbms_xmlgen.getxml('select count(case when (' ||
                                                     column_name || ' = '''' ) then 0 end) 
                                                                             as c '||
                                                                      --^^^^ these quotes
                                                     'from "' || owner ||
                                                     '"."' || table_name || '"'))
                          returning content)) as null_count,
       to_number(xmlquery('/ROWSET/ROW/C/text()' passing
                          xmltype(dbms_xmlgen.getxml('select count(case when "' ||
                                                     column_name ||
                                                     '" is not null then 1 end) as c ' ||
                                                     'from "' || owner ||
                                                     '"."' || table_name || '"'))
                          returning content)) as notnull_count
  from all_tab_columns
 where owner = 'JAMES'
   and table_name = 'TEST'
   and data_type in ('NUMBER','DATE','TIMESTAMP','CHAR','VARCHAR2','NCHAR','NVARCHAR2');