在一个 plsql 块中创建和使用 table
create and use table within one plsql block
我正在尝试为长 运行ning 语句创建用户定义的 oracle 云控制警报。所以这就是我无法在时钟执行之前执行某些特定操作(例如创建所需 table)的原因。有一个问题:
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 中我创建 table LONG_SQL 如果它不存在然后我想使用这个新的 table.你能在这里提出一些解决方法吗?是否可以在一个 plsql 块中创建然后使用 table?
生于动态SQL,死于动态SQL。
如果您动态创建 table,则您也必须动态使用它。换句话说,这个查询:
SELECT COUNT(*)
INTO ALREADY_EXISTS
FROM LONG_SQL ...
也必须是 动态 并使用 execute immediate
.
执行
我正在尝试为长 运行ning 语句创建用户定义的 oracle 云控制警报。所以这就是我无法在时钟执行之前执行某些特定操作(例如创建所需 table)的原因。有一个问题:
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 中我创建 table LONG_SQL 如果它不存在然后我想使用这个新的 table.你能在这里提出一些解决方法吗?是否可以在一个 plsql 块中创建然后使用 table?
生于动态SQL,死于动态SQL。
如果您动态创建 table,则您也必须动态使用它。换句话说,这个查询:
SELECT COUNT(*)
INTO ALREADY_EXISTS
FROM LONG_SQL ...
也必须是 动态 并使用 execute immediate
.