使用DECLARE变量在Oracle PL/SQL上执行过程

使用DECLARE变量在Oracle PL/SQL上执行过程,oracle,stored-procedures,plsql,Oracle,Stored Procedures,Plsql,我目前正在学习Oracle PL/SQL。我想用变量创建存储过程,然后用另一个脚本调用它。可能吗 我尝试使用不带变量的简单脚本,它可以工作: CREATE OR REPLACE PROCEDURE testmyproc AS BEGIN INSERT INTO tes_table(dt) VALUES (sysdate); commit; END testmyproc; 然后我用另一个脚本abc.sql调用它 begin testmyproc; end; 它成功地工作了 但,不幸的是,若

我目前正在学习Oracle PL/SQL。我想用变量创建存储过程,然后用另一个脚本调用它。可能吗

我尝试使用不带变量的简单脚本,它可以工作:

CREATE OR REPLACE PROCEDURE testmyproc AS
BEGIN
INSERT INTO tes_table(dt)
VALUES (sysdate);
commit;
END testmyproc;
然后我用另一个脚本abc.sql调用它

begin
  testmyproc;
end;
它成功地工作了

但,不幸的是,若我在我的过程中使用DECLARE(variable),它在执行时会显示错误(但在create过程中它会成功)

以下是我的程序(无错误):

这是我的执行脚本abc.sql(执行时显示错误)

-未经申报

begin
  sp_testmyproc;
end;
-我试着用DECLARE执行它

DECLARE
  job_name varchar(100);
  status_key number;
  status_desc varchar(100);
  notes varchar(250);
begin
  status_key := 1;
  status_desc := 'SUCCESS';
  notes := 'Process Completed';
  sp_testmyproc;
end;
它显示如下错误:

>     ORA-06550: line 8, column 11:
>     PLS-00905: SP_TESTMYPROC is invalid
>     ORA-06550: line 8, column 3:
>     PL/SQL: Statement ignored
我可以调用另一个脚本的过程吗?这是最佳实践吗? 我只是认为这个过程可以用于很多情况(比如编程中的函数)


谢谢大家!

您需要学习该过程的语法

在过程中,不应使用关键字
DECLARE
。要声明的任何变量必须介于过程中的
AS
BEGIN
之间

您的过程应如下所示:

CREATE OR REPLACE PROCEDURE sp_testmyproc AS
--DECLARE
   job_name varchar(100);
   status_key number;
   status_desc varchar(100);
   notes varchar(250);
BEGIN
    status_key := 1;
    .....
    .....
    .....

有关oracle过程的语法,请参阅此部分,因为它非常容易理解。

请注意在SQL*Plus中创建存储过程时的区别:

SQL> create or replace procedure test_ok as
  2   v number;
  3  begin
  4   v:=0;
  5  end;
  6  /

Procedure created.

SQL> show errors
No errors.


SQL> create or replace procedure test_ko as
  2  declare
  3   v number;
  4  begin
  5   v:=0;
  6  end;
  7  /

Warning: Procedure created with compilation errors.

SQL> show errors
Errors for PROCEDURE TEST_KO:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1  PLS-00103: Encountered the symbol "DECLARE" when expecting one of
     the following:
     begin function pragma procedure subtype type <an identifier>
     <a double-quoted delimited-identifier> current cursor delete
     exists prior external language

SQL> 
SQL>创建或替换过程测试\u正常
2 v数字;
3开始
4V:=0;
5结束;
6  /
程序已创建。
SQL>显示错误
没有错误。
SQL>创建或替换过程测试作为
2申报
3V数字;
4开始
5V:=0;
6结束;
7  /
警告:创建的过程存在编译错误。
SQL>显示错误
程序测试_KO的错误:
行/列错误
-------- -----------------------------------------------------------------
2/1 PLS-00103:在期望下列情况之一时遇到符号“DECLARE”
以下是:
begin函数pragma过程子类型类型
当前光标删除
存在于外部语言之前
SQL>

如果您有编译错误,您至少会收到
警告:使用编译错误创建的过程。
如果您有编译错误并使用
显示错误,您会收到所有错误消息。

我有两个过程要这样运行,当我在不声明的情况下更改过程时,一个执行成功,另一个仍有相同的错误。。你对错误或这个限制有什么想法吗?我认为这是完全不同的问题。将其标记为已接受,并询问其他问题以避免单个问题中出现任何混乱。我运行创建过程并通过PL/SQL Developer执行它。我是否需要使用DECLARE来执行它?不能在存储过程中使用DECLARE:这不取决于使用的客户端工具。它只依赖于在数据库服务器端运行的PL/SQL。
SQL> create or replace procedure test_ok as
  2   v number;
  3  begin
  4   v:=0;
  5  end;
  6  /

Procedure created.

SQL> show errors
No errors.


SQL> create or replace procedure test_ko as
  2  declare
  3   v number;
  4  begin
  5   v:=0;
  6  end;
  7  /

Warning: Procedure created with compilation errors.

SQL> show errors
Errors for PROCEDURE TEST_KO:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1  PLS-00103: Encountered the symbol "DECLARE" when expecting one of
     the following:
     begin function pragma procedure subtype type <an identifier>
     <a double-quoted delimited-identifier> current cursor delete
     exists prior external language

SQL>