运行 Oracle 存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1520117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:08:00  来源:igfitidea点击:

Running Oracle stored Procedure

oracle

提问by Colin Pickard

I have a sql file that contains a simple procedure to print "Hi" like,

我有一个 sql 文件,其中包含一个简单的程序来打印“嗨”,例如,

CREATE OR REPLACE PROCEDURE skeleton 
IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hi');
END;

When I try to execute this from sql file itself, it just gets compiled and it is not running.

当我尝试从 sql 文件本身执行它时,它只是被编译并且没有运行。

I added the as,

我添加了作为,

CREATE OR REPLACE PROCEDURE skeleton 
IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hi');
END;
/ /* this is required else it throws me compilation error*/
set serveroutput on
EXECUTE skeleton;

I tried calling the same skeleton from other sql file also and even from the sqldeveloper GUI, that also didnt execute this. Only the sqlplus commandline helps me. Let me know what I am missing and the solution for this.

我也尝试从其他 sql 文件甚至从 sqldeveloper GUI 调用相同的框架,但也没有执行此操作。只有 sqlplus 命令行对我有帮助。让我知道我缺少什么以及解决方案。

回答by Ian Carpenter

Here are the steps I took using SQL Plus

以下是我使用 SQL Plus 采取的步骤

SQL> CREATE OR REPLACE PROCEDURE skeleton
  2  IS
  3  BEGIN
  4    DBMS_OUTPUT.PUT_LINE('Hi');
  5  END;
  6  /

Procedure created.

SQL> set serveroutput on
SQL> EXECUTE skeleton;
Hi

PL/SQL procedure successfully completed.

SQL>

Can you start a new sqlplus session replicate these steps and post the content?

你能启动一个新的 sqlplus 会话复制这些步骤并发布内容吗?

回答by Colin Pickard

The only change I had to make to your sql to allow running it as an @file was to remove the comment. This is the whole content of the the .sql file:

我必须对您的 sql 进行的唯一更改以允许将其作为 @file 运行,就是删除注释。这是 .sql 文件的全部内容:

CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hi');
END;
/
set serveroutput on
EXECUTE skeleton;

You should get an output something like this:

你应该得到这样的输出:

C:\Temp>sqlplus username/password @skeleton.sql

SQL*Plus: Release 11.1.0.6.0 - Production on Mon Oct 5 17:10:46 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Procedure created.

Hi

PL/SQL procedure successfully completed.

SQL>

回答by Colin Pickard

In SQL Developer, you need the "execute script" button which is the second button at the top left (little green arrow in front of document) instead of the one with the big green arrow.

在 SQL Developer 中,您需要“执行脚本”按钮,它是左上角的第二个按钮(文档前面的小绿色箭头),而不是带有大绿色箭头的按钮。

That threw me the first time I used it.

我第一次使用它时,这让我很震惊。

SQL Developer
(source: devshed.com)

SQL 开发人员
(来源:devshed.com

The reason your first example gets compiled and not run, is that you are not actually asking it to run. You are creating a procedure inside the database, which works correctly, but not calling it. In your second block you have an EXECUTEwhich is where it actually runs.

您的第一个示例被编译而不运行的原因是您实际上并没有要求它运行。您正在数据库中创建一个过程,该过程可以正常工作,但没有调用它。在你的第二个块中,你有一个EXECUTE它实际运行的地方。

回答by Xaisoft

Try setting set serveroutput on before the dbms_output.

尝试在 dbms_output 之前设置 set serveroutput on。