为啥我们要编写 create/replace 来在 PL/SQL 中创建过程

Posted

技术标签:

【中文标题】为啥我们要编写 create/replace 来在 PL/SQL 中创建过程【英文标题】:why do we write create/replace for creating a procedure in PL/SQL为什么我们要编写 create/replace 来在 PL/SQL 中创建过程 【发布时间】:2011-03-07 10:54:52 【问题描述】:

谁能解释一下为什么我们要编写 CREATE OR REPLACE 来在 PL/SQL 中创建存储过程?

【问题讨论】:

【参考方案1】:

OR REPLACE 允许您替换已经存在的过程,换句话说,您不需要在每次想要重新创建它时删除该过程并重新创建它

【讨论】:

【参考方案2】:

REPLACE KEYWORD 允许您修改已经存在的数据库对象。

看看下面的例子,你就明白了。

CREATE PROCEDURE pr_greetings
IS 
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hello');
END;

输出:

PROCEDURE PR_GREETINGS compiled

如果我们尝试在没有 REPLACE KEYWORD 的情况下进行修改,则会出现错误, 所以我应该放弃它并重新创建它。 见下文。

CREATE PROCEDURE pr_greetings              ``
IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hello World');
END;

OUTPUT:
ORA-00955: name is already used by an existing object

现在使用 REPLACE KEYWORD 我们可以修改它

CREATE or REPLACE PROCEDURE pr_greetings
IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hello World');
END;

OUTPUT:
PROCEDURE PR_GREETINGS compiled.

希望你理解清楚, 谢谢。

【讨论】:

【参考方案3】:

1 要在不使用“创建或替换”的情况下修改过程,您必须分两步删除并重新创建对象本身。

2 主要原因是为了保留对象的授权:

SQL> connect to hr
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as hr

SQL> 
SQL> create procedure dummy
  2  as
  3  begin
  4      null;
  5  end dummy;
  6  /
Procedure created


SQL> grant execute on dummy to bps;

Grant succeeded

SQL> connect bps
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as bps

SQL> select * from user_tab_privs_recd where table_name ='DUMMY';

OWNER                          TABLE_NAME                     GRANTOR                        PRIVILEGE                                GRANTABLE HIERARCHY
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- ---------
HR                             DUMMY                          HR                             EXECUTE                                  NO        NO
SQL> exec hr.dummy;

PL/SQL procedure successfully completed

SQL> connect hr
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as hr

SQL> create or replace procedure dummy
  2  as
  3  begin
  4      null;
  5      dbms_output.put_line('dummy');
  6  end;
  7  /

Procedure created

SQL> select * from user_tab_privs_made where table_name ='DUMMY';

GRANTEE                        TABLE_NAME                     GRANTOR                        PRIVILEGE                                GRANTABLE HIERARCHY
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- ---------
BPS                            DUMMY                          HR                             EXECUTE                                  NO        NO

SQL> connect bps
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as bps

SQL> 
SQL> select * from user_tab_privs_recd where table_name ='DUMMY';

OWNER                          TABLE_NAME                     GRANTOR                        PRIVILEGE                                GRANTABLE HIERARCHY
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- ---------
HR                             DUMMY                          HR                             EXECUTE                                  NO        NO


SQL> set serveroutput on
SQL> exec hr.dummy;

dummy

PL/SQL procedure successfully completed

SQL> connect hr
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as hr
 SQL> drop procedure dummy;

Procedure dropped
SQL> create  procedure dummy
  2  as
  3  begin
  4      null;
  5      dbms_output.put_line('dummy');
  6  end;
  7  /

Procedure created
SQL> -- as you can see priviliges previously made are gone
SQL> select * from user_tab_privs_made where table_name ='DUMMY';

GRANTEE                        TABLE_NAME                     GRANTOR                        PRIVILEGE                                GRANTABLE HIERARCHY
------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- ---------

SQL> connect bps
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 
Connected as bps
SQL> select * from user_tab_privs_recd where table_name ='DUMMY';

OWNER                          TABLE_NAME                   GRANTOR                        
------------------------------ ---------------------------------------- 
SQL> exec hr.dummy;

begin hr.dummy; end;

ORA-06550: line 2, column 7:
PLS-00201: identifier 'HR.DUMMY' must be declared
ORA-06550: line 2, column 7:
PL/SQL: Statement ignored

SQL> 

【讨论】:

以上是关于为啥我们要编写 create/replace 来在 PL/SQL 中创建过程的主要内容,如果未能解决你的问题,请参考以下文章

为啥我们需要一个默认构造函数来在 C++ 中通过引用传递一个对象?

为啥 g++ 需要 -fpermissive 标志来在 c++ 中打印迭代器值? [关闭]

为啥我需要一个 QTranslator 来在 Windows 上的 QMessageBox 中本地化按钮文本?

为啥需要方括号来在 Javascript 中对 Map 的所有元素进行字符串化?

如果 Swift 通过值而不是引用传递,为啥我可以通过将 UIView 传递给函数来在 Swift 中操作 UIView 的属性? [复制]

当 asm.js 比普通的 JS 代码快时,我为啥要在 JS 中编写新代码?