要求解释此 PL/SQL 代码
Asking for explaination of this PL/SQL code
我想问一下这个PL/SQL代码到底是做什么的:
DECLARE
table_doesnot_exists EXCEPTION;
PRAGMA EXCEPTION_INIT (table_doesnot_exists, -942);
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE TABLE_NAME';
EXCEPTION
WHEN table_doesnot_exists
THEN NULL;
END;
我部分理解这个,但我对此很好奇-942
。
只是table的oracle异常代码不存在
来自 oracle 文档:
ORA-00942 table or view does not exist
Cause: The table or view entered does not exist, a synonym that is not allowed here was used, or a view was referenced where a table is required. Existing user tables and views can be listed by querying the data dictionary. Certain privileges may be required to access the table. If an application returned this message, the table the application tried to access does not exist in the database, or the application does not have access to it.
了解更多信息http://www.techonthenet.com/oracle/errors/ora00942.php
我已经浏览并评论了该区块。基本上你的代码测试自定义错误处理。
DECLARE
--Define the custom exception
table_doesnot_exists EXCEPTION;
/*
*In order to use the custom exception we have to declare it (typically used with ORA messages
*that have no predefined name. Syntax: PRAGMA EXCEPTION_INIT (exception_name, -ORA_ERR_#);
*In this case: 00942, 00000, "table or view does not exist"
*Original code cited 942 but the oerr code is actually 00942, the leading 0's in this case are irrelevant
*/
PRAGMA EXCEPTION_INIT (table_doesnot_exists, -942);
BEGIN
--Attempt to drop the table
EXECUTE IMMEDIATE 'DROP TABLE test_table_does_not_exist';
EXCEPTION
--If the table we attempted does not exist then our custom exception will catch the ORA-00942
WHEN table_doesnot_exists
--Now lets throw out a debug output line
THEN DBMS_OUTPUT.PUT_LINE('table does not exist');
END;
/
它删除 table,如果在尝试删除它时 table 不存在,它会捕获相应的错误(ORA-00942 = -942 in EXCEPTION_INIT Pragma ) 然后什么也不做。
如果您使用的是另一个 RDBMS(例如 Postgres),您最好使用“DROP TABLE IF EXISTS my_table”,但遗憾的是 Oracle 不提供此语法。
我想问一下这个PL/SQL代码到底是做什么的:
DECLARE
table_doesnot_exists EXCEPTION;
PRAGMA EXCEPTION_INIT (table_doesnot_exists, -942);
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE TABLE_NAME';
EXCEPTION
WHEN table_doesnot_exists
THEN NULL;
END;
我部分理解这个,但我对此很好奇-942
。
只是table的oracle异常代码不存在
来自 oracle 文档:
ORA-00942 table or view does not exist
Cause: The table or view entered does not exist, a synonym that is not allowed here was used, or a view was referenced where a table is required. Existing user tables and views can be listed by querying the data dictionary. Certain privileges may be required to access the table. If an application returned this message, the table the application tried to access does not exist in the database, or the application does not have access to it.
了解更多信息http://www.techonthenet.com/oracle/errors/ora00942.php
我已经浏览并评论了该区块。基本上你的代码测试自定义错误处理。
DECLARE
--Define the custom exception
table_doesnot_exists EXCEPTION;
/*
*In order to use the custom exception we have to declare it (typically used with ORA messages
*that have no predefined name. Syntax: PRAGMA EXCEPTION_INIT (exception_name, -ORA_ERR_#);
*In this case: 00942, 00000, "table or view does not exist"
*Original code cited 942 but the oerr code is actually 00942, the leading 0's in this case are irrelevant
*/
PRAGMA EXCEPTION_INIT (table_doesnot_exists, -942);
BEGIN
--Attempt to drop the table
EXECUTE IMMEDIATE 'DROP TABLE test_table_does_not_exist';
EXCEPTION
--If the table we attempted does not exist then our custom exception will catch the ORA-00942
WHEN table_doesnot_exists
--Now lets throw out a debug output line
THEN DBMS_OUTPUT.PUT_LINE('table does not exist');
END;
/
它删除 table,如果在尝试删除它时 table 不存在,它会捕获相应的错误(ORA-00942 = -942 in EXCEPTION_INIT Pragma ) 然后什么也不做。
如果您使用的是另一个 RDBMS(例如 Postgres),您最好使用“DROP TABLE IF EXISTS my_table”,但遗憾的是 Oracle 不提供此语法。