" ORA-01007:变量不在选择列表中#34;当EXECUTE IMMEDIATE

时间:2017-04-05 10:32:10

标签: oracle dynamic plsql execute-immediate

我有一个过程,它接收一个where子句(即where col1 = 1)作为参数。我使用此子句在一些表中使用EXECUTE IMMEDIATE语句进行搜索,并将结果插入到嵌套表中,然后显示。

如果找到任何数据,该过程正常,但如果没有找到数据,则抛出上述错误。

有人可以解释导致此错误的原因吗?

以下是程序:

create or replace procedure prc_checks(pi_where varchar2) as

    cursor c_tables is
        select object_name, 
        case object_name
            when 'XP_IMPORT_MW' THEN 99999999
            when 'XP_IMPORT_MW_ARCH' THEN 99999998
            else TO_NUMBER(SUBSTR(object_name, -8, 8))
        end to_order
        from dba_objects 
        where object_type = 'TABLE' 
        and object_name IN ('XP_IMPORT_MW', 'XP_IMPORT_MW_ARCH') 
        or REGEXP_LIKE (object_name, 'XP_IMPORT_MW_ARCH_201(5|6|7)[0-9]{4}') order by 2 desc;

    type t_result is table of xp_import_mw%rowtype;
    v_result t_result;

    v_sql varchar2(300);

BEGIN  
    for i in c_tables
        loop
            v_sql := 'select * from ' || i.object_name || ' ' || pi_where;
            execute immediate v_sql bulk collect into v_result;

            if v_result.count > 0 
                then 
                    for j in v_result.first .. v_result.last
                    loop
                        dbms_output.put_line(v_result(j).art_nr);
                    end loop;
                    dbms_output.put_line('... the required information was found on table name ' || upper(i.object_name));
                    exit;
            end if;
        end loop;
END prc_checks;

1 个答案:

答案 0 :(得分:2)

您将得到这个是光标找到的表之一,其列数少于xp_import_mw。例如:

create table xp_import_mw (col1 number, art_nr number, dummy number);
create table xp_import_mw_arch_20160102 (col1 number, art_nr number, dummy number);
create table xp_import_mw_arch_20160101 (col1 number, art_nr number);
insert into xp_import_mw_arch_20160101 values (1, 42); 

因此主xp_import_mw表有三列但没有匹配的数据。其中一个旧存档表的列数较少。

我在程序中添加了dbms_output.put_line(v_sql)以查看它失败的表,然后运行它:

set serveroutput on
exec prc_checks('where col1 = 1');

得到了输出:

select * from XP_IMPORT_MW where col1 = 1
select * from XP_IMPORT_MW_ARCH_20160102 where col1 = 1
select * from XP_IMPORT_MW_ARCH_20160101 where col1 = 1

Error starting at line : 49 in command -
BEGIN prc_checks('where col1 = 1'); END;
Error report -
ORA-01007: variable not in select list
ORA-06512: at "MY_SCHEMA.PRC_CHECKS", line 25
ORA-06512: at line 1
01007. 00000 -  "variable not in select list"
*Cause:    
*Action:

所以问题不在于没有找到数据;问题是匹配表中具有错误结构的数据。

您可以根据xp_import_mw表的结构构建选择列表,而不是使用*;它不会让它失败,但至少会给你一个稍微有用的错误信息 - 在这种情况下是ORA-00904: "DUMMY": invalid identifier而不是ORA-01007。

您可以通过以下方式快速粗略地检查是否存在差异:

select table_name, count(column_id) as column_count,
  listagg(column_name, ',') within group (order by column_id) as columns
from dba_tab_columns
where table_name IN ('XP_IMPORT_MW', 'XP_IMPORT_MW_ARCH') 
or REGEXP_LIKE (table_name, 'XP_IMPORT_MW_ARCH_201(5|6|7)[0-9]{4}')
group by table_name
having count(column_id) != (
  select count(column_id) from dba_tab_columns where table_name = 'XP_IMPORT_MW'
);

...虽然如果您正在使用dba_*all_*视图,那么您应该在此处和程序中包含所有者。