0

我正在尝试为长期运行的语句创建一个用户定义的警报,用于 Oracle 云控制。所以这就是我不能在时钟执行之前做一些特定的动作(比如创建所需的表)的原因。有一个问题:

Error report -
ORA-06550: line 32, column 10:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 30, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

当我运行这段代码时:

SET SERVEROUTPUT ON
DECLARE
    -- VARIABLES
    sql_id_var V$SESSION.sql_id%TYPE;
    SQL_CHILD_NUMBER_var  V$SESSION.SQL_CHILD_NUMBER%TYPE;
    ALREADY_EXISTS NUMBER := 0;

    --TO GET SQL_ID,CHILD_ID
    CURSOR  sql_id_cursor IS
    select ses.sql_id, ses.SQL_CHILD_NUMBER
    from V$SESSION ses
    where SES.STATUS = 'ACTIVE'
    and SES.USERNAME is not null
    --and Ses.AUDSID <> userenv('SESSIONID') 
    and ses.sql_id is not null
    and (SYSDATE - ses.SQL_EXEC_START) > 5/(24*60*60) --SECONDS;

begin

 --CREATE TABLE TO STORE SQL_ID OF LONG RUNNING QUERIES
 EXECUTE IMMEDIATE 'CREATE TABLE LONG_SQL (SQL_ID VARCHAR2(13),
                     SQL_CHILD_NUMBER NUMBER,
                     DETECTED_DATE DATE)' ;
  exception when others then
  if SQLCODE = -00955 then null;dbms_output.put_line( 'TABLE EXISTS ' ); else raise; end if;


  --PROCESSING EVERY LONG QUER
  FOR sql_id_row IN sql_id_cursor
  LOOP
    dbms_output.put_line( 'SQL_ID: ' || sql_id_row.sql_id || ' , SQL_CHILD_NUMBER: ' ||  sql_id_row.SQL_CHILD_NUMBER || ' , NOW IS :' || SYSDATE);
    SELECT COUNT(*)
    INTO ALREADY_EXISTS
    FROM LONG_SQL
    WHERE LONG_SQL.SQL_ID = sql_id_row.sql_id
    AND LONG_SQL.SQL_CHILD_NUMBER = sql_id_row.SQL_CHILD_NUMBER;
    dbms_output.put_line( 'SQL_ID: ' || sql_id_row.sql_id || ' , SQL_CHILD_NUMBER: ' ||  sql_id_row.SQL_CHILD_NUMBER || ' , FOUND ' || ALREADY_EXISTS || ' TIMES INTO LOG TABLE');

  END LOOP;


end;
/

在这个 plsql 中,如果表LONG_SQL不存在,我将创建它,然后我想进行一些使用这个新表的查询。你能在这里提出一些解决方法吗?是否可以在一个 plsql 块中创建然后使用表?

4

1 回答 1

2

以动态 SQL 为生的人,以动态 SQL 为生。

如果您动态创建表,则还必须动态使用它。换句话说,这个查询:

SELECT COUNT(*)
    INTO ALREADY_EXISTS
    FROM LONG_SQL ...

还必须是动态的并使用execute immediate.

于 2020-02-26T13:30:43.657 回答