Stored procedures 为什么要编写create/replace来在PL/SQL中创建过程

Stored procedures 为什么要编写create/replace来在PL/SQL中创建过程,stored-procedures,syntax,plsql,Stored Procedures,Syntax,Plsql,有人能解释一下为什么我们要编写CREATE或REPLACE来在PL/SQL中创建存储过程吗?或REPLACE允许您替换已经存在的过程,换句话说,您不需要每次重新创建过程时都删除并重新创建该过程1要在不使用“创建或替换”的情况下修改过程,您必须分两步删除并重新创建对象本身 2主要原因是为了保存对象授权: SQL> connect to hr Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 Connect

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

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

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> 

REPLACE关键字允许您修改已经存在的数据库对象

考虑一下下面的例子,你就会明白了

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

PROCEDURE PR_GREETINGS compiled
如果我们尝试在不替换关键字的情况下进行修改,则会出现错误, 所以我应该放弃它,重新创建它。 见下文

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关键字我们可以修改它

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

OUTPUT:
PROCEDURE PR_GREETINGS compiled.
希望你能明白,, 多谢各位