如何使用Oracle SQL开发人员运行存储过程?

Ham*_*jan 19 oracle plsql stored-procedures sys-refcursor oracle-sqldeveloper

*EDIT6:*这最终为我工作(从接受的答案):

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor
exec :tran_cnt := 0
exec :msg_cnt := 123
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => :rc)
print :tran_cnt
print :msg_cnt
print :rc
Run Code Online (Sandbox Code Playgroud)

SQL Developer让这个超级难/不可能?我不在乎该实用程序是否基于命令行; 我只是希望能够快速运行并查看它.如果它也能很好地捕获错误,那就太好了.能够逐步(交互式)登录,以及一次性指定所有内容(类似于典型的基于ftp/sftp cmd的客户端的工作方式)将会很棒.

我的平台是Windows Server 2008 + Cygwin.

编辑:也许你会知道如何使用Python编写脚本?

编辑2:在MSFT SQL服务器中,我只需输入:

get_user 1;
Run Code Online (Sandbox Code Playgroud)

然后突出显示它并点击F5,我得到:

login   name    
NULL    Somename
Run Code Online (Sandbox Code Playgroud)

打印到输出窗口.Oracle SQL开发人员根本没有帮助.我不知道如何传入1,我不知道如何查看返回的实际行/记录.

编辑3:当我输入var rc refcursor;并选择并运行它时,我收到此错误(GUI):

An error was encountered performing the requested operation:

ORA-00900: invalid SQL statement
00900.00000 - "invalid SQL statement"
* Cause:
* Action:
Vendor code 900Error at Line: 2
Run Code Online (Sandbox Code Playgroud)

EDIT4:

我正在尝试运行一个过程,其定义如下所示:

create or replace procedure get_account
(
    Vret_val out number,
    Vtran_count in out number,
    Vmessage_count in out number,
    Vaccount_id     IN NUMBER
    , rc1 in out sys_refcursor
)as
begin
...
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Error starting at line 2 in command:
exec :rc := get_account(1) 
Error report:
ORA-06550: line 1, column 24:
PLS-00306: wrong number or types of arguments in call to 'GET_ACCOUNT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
rc
------
Run Code Online (Sandbox Code Playgroud)

我很亲密......请帮忙.

*编辑5:*

我正在运行的脚本(功能相同),错误总是一样的:

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor
exec :tran_cnt := 0
exec :msg_cnt := 123
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)
Run Code Online (Sandbox Code Playgroud)

脚本输出(在F5上)(可能是几次运行的几条消息.):

Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
anonymous block completed
anonymous block completed

Error starting at line 7 in command:
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)
Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
anonymous block completed
anonymous block completed

Error starting at line 7 in command:
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)
Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
Run Code Online (Sandbox Code Playgroud)

为什么说第1行,第134列?没有线延伸到那么远......

APC*_*APC 35

不仅有办法做到这一点,有不止一种方法可以做到这一点(我承认不是非常Pythonic,但SQL*Developer是用Java编写的).

我有一个带有此签名的程序:get_maxsal_by_dept( dno number, maxsal out number).

我在SQL*Developer Object Navigator中突出显示它,调用右键单击菜单并选择Run.(我可以使用ctrl+ F11.)这会产生一个带有测试工具的弹出窗口.(注意:如果存储过程存在于包中,则需要右键单击包,而不是包含过程名称的包下面的图标;然后在测试时从包的"目标"列表中选择sproc出现线束.)在此示例中,测试工具将显示以下内容:

DECLARE
  DNO NUMBER;
  MAXSAL NUMBER;
BEGIN
  DNO := NULL;

  GET_MAXSAL_BY_DEPT(
    DNO => DNO,
    MAXSAL => MAXSAL
  );
  DBMS_OUTPUT.PUT_LINE('MAXSAL = ' || MAXSAL);
END;
Run Code Online (Sandbox Code Playgroud)

我将变量DNO设置为50并按下确定.在Running - Log窗格(右下角,除非你关闭/移动/隐藏它)我可以看到以下输出:

Connecting to the database apc.
MAXSAL = 4500
Process exited.
Disconnecting from the database apc. 
Run Code Online (Sandbox Code Playgroud)

为了公平起见,对于返回Ref Cursor的函数,运行器不太友好,就像这样:get_emps_by_dept (dno number) return sys_refcursor.

DECLARE
  DNO NUMBER;
  v_Return sys_refcursor;
BEGIN
  DNO := 50;

  v_Return := GET_EMPS_BY_DEPT(
    DNO => DNO
  );
  -- Modify the code to output the variable
  -- DBMS_OUTPUT.PUT_LINE('v_Return = ' || v_Return);
END;
Run Code Online (Sandbox Code Playgroud)

但是,至少它提供了保存文件更改的机会,因此我们可以保留投资来调整线束......

DECLARE
  DNO NUMBER;
  v_Return sys_refcursor;
  v_rec emp%rowtype;
BEGIN
  DNO := 50;

  v_Return := GET_EMPS_BY_DEPT(
    DNO => DNO
  );

  loop
    fetch v_Return into v_rec;
    exit when v_Return%notfound;
    DBMS_OUTPUT.PUT_LINE('name = ' || v_rec.ename);
  end loop;
END;
Run Code Online (Sandbox Code Playgroud)

来自同一位置的输出:

Connecting to the database apc.
name = TRICHLER
name = VERREYNNE
name = FEUERSTEIN
name = PODER
Process exited.
Disconnecting from the database apc. 
Run Code Online (Sandbox Code Playgroud)

或者,我们可以在SQL Developer工作表中使用旧的SQL PLus命令:

var rc refcursor 
exec :rc := get_emps_by_dept(30) 
print rc
Run Code Online (Sandbox Code Playgroud)

在这种情况下,输出将显示在" 脚本输出"窗格中(默认位置是" 结果"选项卡右侧的选项卡).

最早的IDE版本对SQL*Plus的支持并不多.但是,自1.2.1以来,所有上述命令都得到了支持.有关详细信息,请参阅联机文档中的矩阵.


"当我输入var rc refcursor; 并选择并运行它时,我收到此错误(GUI):"

工作表解释SQL Plus命令的方式有一个功能或缺陷.它假定SQL Plus命令是脚本的一部分.因此,如果我们输入一行SQL*Plus,说var rc refcursor并单击Execute Statement(或F9)工作表就会抛出ORA-900,因为这不是可执行语句,即它不是SQL.我们需要做的是单击Run Script(或F5),即使对于单行SQL*Plus也是如此.


"我很亲密......请帮忙."

您的程序是一个具有五个必需参数的签名的过程.您收到错误是因为您将其作为函数调用,并且只有一个参数:

exec :rc := get_account(1)
Run Code Online (Sandbox Code Playgroud)

您需要的是以下内容.为清晰起见,我使用了命名符号.

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor

exec :tran_cnt := 0
exec :msg_cnt := 123

exec get_account (Vret_val => :ret1, 
                  Vtran_count => :tran_cnt, 
                  Vmessage_count => :msg_cnt, 
                  Vaccount_id   => 1,
                  rc1 => :rc )

print tran_count 
print rc
Run Code Online (Sandbox Code Playgroud)

也就是说,每个OUT或IN OUT参数都需要一个变量.IN参数可以作为文字传递.前两个EXEC语句为一些IN OUT参数赋值.第三个EXEC调用该程序.过程不返回值(与函数不同),因此我们不使用赋值语法.最后,此脚本显示映射到OUT参数的几个变量的值.