문제

즉시 호출을 실행하면 '변수가 선택 목록에 없음' 오류가 발생합니다.

안녕하세요, 저는 collectionsexecute immediate를 plsql에서 결합하여 연습하고 있으며 다음 코드가 있습니다.

set serveroutput on;
declare
  type TTabList  is table of table1%rowtype index by pls_integer;
  vtabList       TTabList;
  vstrQuery      varchar2(32000);
  vNum           number(10)     := 100;
  vStrs          varchar2(100)  := '''45'',''11'',''20'',''71''';
begin
  dbms_output.enable;
  vstrQuery := 'select field1
                  from table1
                 where field1 =   '|| vNum ||'
                   and field2 in ('|| vStrs ||')';

  execute immediate vstrQuery bulk collect into vtabList;  

  if vtabList.count = 0 then
    dbms_output.put_line('nothing to show!');
  else
    while vtabList.count > 0 loop
      for i in vtabList.first .. vtabList.last loop
        dbms_output.put_line('we have: '|| vtabList(i).field1);
      end loop;
      vtabList.delete;
    end loop;
  end if;
end;

하지만 내가 해결할 수 없는 오류 다음의 결과:

Error report - 
ORA-01007: variable not in select list
ORA-06512: at line 14
01007. 00000 -  "variable not in select list"
*Cause:    
*Action:

이 문제를 해결할 아이디어가 있습니까?

도움이 되었습니까?

해결책

mustaccio 가 지적했듯이 유형은 동일해야 합니다.TTabList 정의를 하나의 필드만 수집하도록 변경하거나(아래 예 참조) 다음을 선택해야 합니다. vstrQuery := 'select * ... 다른 모든 것은 rowrype로 잘못된 인쇄를 수정한 후에 작동해야 합니다(rowtype여야 함).예를 들어 field1만 선택하려는 경우 다음을 수행할 수 있습니다.

set serveroutput on;
declare
  type TAB1_FIELD1 IS RECORD (field1 table1.field1%type);
  type TTabList  is table of TAB1_FIELD1 index by pls_integer;
  vtabList       TTabList;
  vstrQuery      varchar2(32000);
  vNum           number(10)     := 100;
  vStrs          varchar2(100)  := '''45'',''11'',''20'',''71''';
begin
  dbms_output.enable;
  vstrQuery := 'select field1
                  from table1
                 where field1 =   '|| vNum ||'
                   and field2 in ('|| vStrs ||')';

  --works fine with collections (at least starting from 10g) 
  execute immediate vstrQuery bulk collect into vtabList; 

  ....
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top