Oracle PLSQL invalid cursor 错误我不明白

Oracle PLSQL invalid cursor error I don't understand

在 PL/SQL 方面,我还是一个相对较新的人。

在 Linux RHEL 6.8 上使用 Oracle 12c,以下 shell 脚本将尝试激活 table 集合中的所有 RI 约束,如果它们因父键而失败失败,它将转储有问题的数据的前 100 行(或更少)。或者至少这是目标。由于该脚本主要处理 12c 上的系统 tables(只有一小部分用户 table 列表是我的安装所特有的),所以我完全包括了我的环境中的所有内容。

主要工作发生在异常处理中,在该异常处理中查询系统 table 的约束,并根据这些数据形成用户查询。

作为一个额外的目标,输出相当混乱,我想清理它,但首先它必须工作:)

我的 tables 得到的输出/错误如下:

ERROR Handling here for table NRNG_MTC_VST Constraint Name: SYS_C0011790 Final SQL = SELECT DISTINCT NRNG_MTC_VST.LOG_CRT_DT , NRNG_MTC_VST.NRRNG_MTC_LG_ID FROM ODB_PRIMARY.NRNG_MTC_VST WHERE NOT EXISTS (SELECT 1 FROM ODB_PRIMARY.NRNG_MTC_LOG WHERE NRNG_MTC_VST.LOG_CRT_DT = NRNG_MTC_LOG.LOG_CRT_DT AND NRNG_MTC_VST.NRRNG_MTC_LG_ID = NRNG_MTC_LOG.NRRNG_MTC_LG_ID) FETCH FIRST 100 rows only ---xxx End SQL DECLARE * ERROR at line 1: ORA-01001: invalid cursor ORA-06512: at line 111 ORA-02298: cannot validate (ODB_PRIMARY.SYS_C0011790) - parent keys not found

我的 print_line 的输出 SQL 是正确的,如果直接粘贴到 SQLDeveloper 会话中也能正常工作。我不明白游标的定义方式有些愚蠢。

剧本全文。 BYW,如果您看到其他与错误无关的愚蠢更改,也请提出建议。

cd $OGGHOME/scripts
export ORACLE_SID=odbod07 $ORACLE_HOME/bin/sqlplus <<-EOF / as sysdba
alter session set container=p01_odbod07; 
set echo on set feedback on
set heading off 
set serveroutput on size 10000

DECLARE    finalsql varchar2(2048);   
part1sql varchar2(1024) ;  
part2sql varchar2(1024) := ' ';   
cownername varchar2(1024);  
ctablename varchar2(1024);   
pownername varchar2(1024);   
ptablename varchar2(1024);   
cnt number := 0;
-- Weak cursor defs   
my_cursor sys_refcursor;

BEGIN   FOR i in (
                select owner, table_name, constraint_name
                from dba_constraints
                where constraint_type = 'R'
                and status = 'DISABLED'
                and owner = 'ODB_PRIMARY'
                and TABLE_NAME in 
-- enter user tables with RI constraints here
('RRNG_MTC_STN_CPLY',   
'NRNG_MTC_VST_MTRL_USG',    
'NRNG_MTC_VST',     
'CAR_CORE',
'NRNG_MTC_LOG')) 
-- end user table definitions, rest of code should rely only on system tables   
LOOP BEGIN
   dbms_output.put_line('alter table '||i.owner|| '.' || 
        i.table_name || ' enable constraint '||i.constraint_name);
   execute immediate 'alter table '||i.owner|| '.' || 
        i.table_name || ' enable constraint '||i.constraint_name;
EXCEPTION
-- exception handling - dump offending data 
WHEN OTHERS THEN      -- take all exceptions for now        
  dbms_output.put_line ('ERROR Handling here for table ' ||
      i.table_name || ' Constraint Name: ' ||i.constraint_name);
  finalsql := 'SELECT DISTINCT ';           
  part1sql := '';           
  part2sql := ' ';          
  cnt := 0;

  for constraint in (           
    SELECT      ucc1.OWNER as childowner,
                ucc1.TABLE_NAME as childtable,
                ucc1.column_name as childcolumn,
                ucc2.OWNER as parentowner,
                ucc2.TABLE_NAME as parenttable,
                ucc2.column_name as parentcolumn,
                utc1.data_type as childdatatype,
                utc1.data_length as childdatalen            
    FROM        all_constraints uc ,
                all_cons_columns ucc1 ,
                all_cons_columns ucc2,
                all_tab_columns utc1            
    WHERE       
                uc.constraint_name       = ucc1.constraint_name
                AND uc.r_constraint_name = ucc2.constraint_name
                AND ucc1.POSITION        = ucc2.POSITION
                AND ucc1.table_name      = utc1.table_name
                AND ucc1.column_name     = utc1.column_name 
                AND uc.constraint_type   = 'R'
                AND uc.constraint_name   = i.constraint_name        
    ORDER BY ucc1.TABLE_NAME , uc.constraint_name)          
    loop
        cownername := constraint.childowner;
        ctablename := constraint.childtable;
        pownername := constraint.parentowner;
        ptablename := constraint.parenttable;
        if cnt > 0 then
            part1sql := part1sql || ' , ';
            part2sql := part2sql || ' AND ';
        end if;
        part1sql := part1sql || constraint.childtable ||
                    '.'||constraint.childcolumn || ' ';
        part2sql := part2sql || constraint.childtable || '.' 
                 || constraint.childcolumn || ' = ' 
                 || constraint.parenttable || '.' ||
                    constraint.parentcolumn;
        cnt := cnt + 1;             
    end loop;

    finalsql := finalsql || part1sql || 
                ' FROM ' ||  ' ' || cownername  || 
                '.' || ctablename ||  
                ' WHERE NOT EXISTS (SELECT 1 FROM ' || 
                pownername || '.' || ptablename || 
               ' WHERE ' || part2sql || ') FETCH FIRST 100 rows only';

    dbms_output.put_line ('Final SQL = ' || finalsql);
    dbms_output.put_line ('---xxx End SQL');            
    open my_cursor for finalsql;            
    dbms_sql.return_result(my_cursor);              
    close my_cursor;
--  EXECUTE IMMEDIATE finalsql;         
END;    
end loop; end; 
/ 
EOF

非常感谢您提供的任何帮助。 布莱恩

您的 FETCH FIRST 100 rows only 似乎不合适。 这是 PL/SQL 中 SELECT 语句中 BULK COLLECT 子句的一部分;据我所知,它不是 SQL 语句的一部分,您可以像这样传递到游标中 这导致游标语句无效

I had same kind of issue when i tried to print Ref_cursor directly. Then i created a Record type variable and then fetched field values in that variable and then i used DBMS_OUTPUT for that record type variable.
Please see if below code and scenario can help you-

set serveroutput on;
declare
v_sql  varchar2(1000);  
v_cursor sys_refcursor;
type myrec is record(col1 varchar2(100),col2 varchar2(1000));
rec myrec;
begin
  v_sql:='select name,status from t_employee where user_id in (''C001117'',''C001122'')'; 

    open v_cursor for v_sql;

  loop
    fetch v_cursor
     into rec;
    exit when v_cursor%notfound;
    dbms_output.put_line( rec.col1||':status  '||rec.col2 );
  end loop;


end;
/

只是为了将其缩小为 simple test case,我认为这是您看到的错误:

declare
    my_cursor sys_refcursor;
begin
    open my_cursor for 'select ''Hello, world'' as message from dual';
    dbms_sql.return_result(my_cursor);
    close my_cursor;  -- << Remove this line
end;
/

ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 6

这是因为您在将游标传递给 dbms_sql 进行处理时试图关闭游标。删除带有 close my_cursor.

的行
declare
    my_cursor sys_refcursor;
begin
    open my_cursor for 'select ''Hello, world'' as message from dual';
    dbms_sql.return_result(my_cursor);
end;
/

PL/SQL procedure successfully completed.

ResultSet #1

MESSAGE
------------
Hello, world

1 row selected.

以下是我的半完整脚本。给定一个 table 列表,它将尝试激活 RI 约束,如果它们失败,它将打印出阻止它被应用的子 table 中的 FK 数据记录。

这个项目最难的部分是 FK 可以是任意数量的任何类型的列,因此在这种情况下打印 select 的结果非常棘手(IMO)。

感谢大家提供的帮助。

cd $OGGHOME/scripts
. ./functions.sh

$ORACLE_HOME/bin/sqlplus ${ORACLE_USERID}/${ORACLE_PASSWORD}@${ORACLE_SID} << EOF 
set echo on
set feedback on
set heading off
set serveroutput on size unlimit

DECLARE 
  finalsql varchar2(2048);
  part1sql varchar2(1024) ;
  part2sql varchar2(1024) := ' ';
  cownername varchar2(1024);
  ctablename varchar2(1024);
  pownername varchar2(1024);
  ptablename  varchar2(1024);
  cnt number := 0;

  desc_tab dbms_sql.desc_tab;
  col_count INTEGER;

  cursor_name INTEGER;

  -- Weak cursor defs
  my_cursor sys_refcursor;
  col1 varchar2(50);
  d     number;
  j     number;
  lineout varchar2(2048);
  plineout varchar2(2048);
  rows number;
  eCount number := 0;


BEGIN
    FOR i in (
                select owner, table_name, constraint_name
                from dba_constraints
                where constraint_type = 'R'
                and status = 'DISABLED'
                and owner = '$DBSCHEMA'
                and TABLE_NAME in (
'RRNG_MTC_STN_CPLY',
'NRNG_MTC_VST_MTRL_USG',
'NRNG_MTC_VST',
'MTC_TSK_HRHY'))
    LOOP
        BEGIN
                dbms_output.put_line ('.');
                dbms_output.put_line ('=====================================');
                dbms_output.put('alter table '||i.owner|| '.' || i.table_name || ' enable constraint '||i.constraint_name);
                execute immediate 'alter table '||i.owner|| '.' || i.table_name || ' enable constraint '||i.constraint_name;
                dbms_output.put_line (' ... SUCCESS');

            EXCEPTION -- exception handling - dump offending data
            WHEN OTHERS THEN
            eCount := eCount + 1;
            dbms_output.put_line (' ... FAILED.  Constraint Name:  ' || i.constraint_name);

            finalsql := 'SELECT DISTINCT ';
            part1sql := '';
            part2sql := ' ';
            cnt := 0;


            for constraint in (
            SELECT      ucc1.OWNER as childowner,
                        ucc1.TABLE_NAME as childtable,
                        ucc1.column_name as childcolumn,
                        ucc2.OWNER as parentowner,
                        ucc2.TABLE_NAME as parenttable,
                        ucc2.column_name as parentcolumn,
                        utc1.data_type as childdatatype,
                        utc1.data_length as childdatalen
            FROM        all_constraints uc ,
                        all_cons_columns ucc1 ,
                        all_cons_columns ucc2,
                        all_tab_columns utc1
            WHERE       
                uc.constraint_name = ucc1.constraint_name
                AND uc.r_constraint_name = ucc2.constraint_name
                AND ucc1.POSITION        = ucc2.POSITION
                AND ucc1.table_name      = utc1.table_name
                AND ucc1.column_name     = utc1.column_name 
                AND uc.constraint_type   = 'R'
                AND uc.constraint_name   = i.constraint_name
            ORDER BY ucc1.TABLE_NAME ,
                     uc.constraint_name)
            loop
                cownername := constraint.childowner;
                ctablename := constraint.childtable;
                pownername := constraint.parentowner;
                ptablename := constraint.parenttable;
                if cnt > 0 then
                    part1sql := part1sql || ' , ';
                    part2sql := part2sql || ' AND ';
                end if;
                part1sql := part1sql || constraint.childtable || '.' || constraint.childcolumn || ' ';
                part2sql := part2sql || constraint.childtable || '.' || constraint.childcolumn || ' = ' 
                            || constraint.parenttable || '.' || constraint.parentcolumn;
                cnt := cnt + 1;
            end loop;

            finalsql := finalsql || part1sql || ' FROM ' ||  ' ' || cownername || '.' || ctablename ||  ' WHERE NOT EXISTS (SELECT 1 FROM ' || 
            pownername || '.' || ptablename || ' WHERE ' || part2sql || ') FETCH FIRST 100 rows only';


            dbms_output.put_line ('Final SQL = (' || finalsql || ')');          
--          dbms_output.put_line ('---xxx End SQL');

            lineout := 'Child Table:  ' || ctablename || '('; 
            plineout := 'Parent Table:  ' || ptablename;

            cursor_name := dbms_sql.open_cursor;
            dbms_sql.PARSE (cursor_name, finalsql, DBMS_SQL.NATIVE);

            d := dbms_sql.execute (cursor_name);


            dbms_sql.describe_columns (cursor_name, col_count, desc_tab);
            for j in 1..col_count
            LOOP
                DBMS_SQL.DEFINE_COLUMN (cursor_name, j, col1, 30);

                lineout := lineout || desc_tab(j).col_name || ' , ';
--              plineout := plineout || constraint.parentcolumn || ' ';
--              dbms_output.put_line ('Column 1: ' || j || ' is ' || desc_tab(j).col_name || ' type ' 
--                      || desc_tab(j).col_type);
            END LOOP j;
        lineout := lineout || ')';
--      plineout := plineout || ')';

        dbms_output.put_line (lineout);
        dbms_output.put_line (plineout);

        lineout := NULL;
        for j in 1..col_count
        LOOP
            if j > 1 then
                lineout := lineout || '        ';
            end if;
            lineout := lineout || desc_tab(j).col_name;
        END LOOP;

        dbms_output.put_line (lineout);

        dbms_output.put_line ('----------------------------------------');

        LOOP
            rows := dbms_sql.fetch_rows (cursor_name);
            EXIT WHEN rows = 0;
            lineout := NULL;

            for j in 1..col_count
            LOOP
                dbms_sql.column_value (cursor_name, j, col1);
                if j > 1 then
                    lineout := ltrim(lineout || '        ' || col1);
                else
                    lineout := col1;
                END IF;
            END LOOP;
            dbms_output.put_line (lineout);
        END LOOP;

        dbms_sql.close_cursor (cursor_name);
        END;
    end loop;
end;
/
EOF