【问题标题】:Oracle SQL to retrieve blank values, nulls, not nulls,distinct values of all columns in a tableOracle SQL 检索表中所有列的空值、空值、非空值、不同值
【发布时间】:2019-09-03 22:44:15
【问题描述】:

我从上一篇文章中选择了这段代码,并尝试调整 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

【问题讨论】:

    标签: sql oracle ora-00907


    【解决方案1】:

    试试这个:

    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-19202:XML 处理中发生错误 ORA-00907:缺少右括号 ORA-06512:在“SYS.DBMS_XMLGEN”,第 176 行 ORA-06512 : at "SYS.DBMS_XMLGEN", line 7 ORA-06512: at "SYS.DBMS_XMLGEN", line 164 ORA-06512: at line 1 19202. 00000 - "Error occurred in XML processing%s" *Cause: An error occurred when处理 XML 函数 *操作:检查给定的错误消息并修复相应的问题
    【解决方案2】:

    似乎不需要一个空长度的字符串(''),并在带引号的字符串中为每个提供一个多引号。因此,您需要将字符串 '' at Line: 9 Column: 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');
    

    【讨论】:

    • 更正后无法获得正确的带有空格的行数。我已经调整为数字 1 的 sql 的 case 运算符添加 to_number(xmlquery('/ROWSET/ROW/C/text()' 传递 xmltype(dbms_xmlgen.getxml('select count(case when (' || column_name | | ' = '''' ) then 1 end) as c '||
    • @Jim 我回答了遇到错误的原因,剩下的就是你的业务逻辑。也许您需要用 is null 表达式替换这些引号。我的意思是你最了解你的业务逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多