【问题标题】:PL SQL Package with Procedure, PLS-00323: subprogram or cursor带有过程的 PL SQL 包,PLS-00323:子程序或游标
【发布时间】:2021-08-11 15:44:42
【问题描述】:

请帮助我。我是 PL SQL 的新手。我想在屏幕上显示所有这些细节

Customer.customerNo, Customer.customerName, Customer.custBalance, customerOrder.orderDate, customerOrder.orderNo

来自 2 个表 [Customer, CustomerOrder]。使用带有过程的唯一包。请帮助我如何创建这样的包?

create or replace package my_pkg as
Procedure getAllOrders2(customer_id IN varchar2);
end my_pkg;
/

create or replace package body my_pkg as
Procedure getAllOrders2(customer_id IN varchar2) is
begin
dbms_output.put_line('customer_id is: '||customer_id );
end getAllOrders2;
end my_pkg;
/

输出应该是:

客户编号、客户名称、订单编号、订单日期、客户余额

Here is the output of all tables

【问题讨论】:

  • 您需要 CustomerOrder 表做什么? “客户详细信息”在客户表中......另外,说您“想要获取客户详细信息” - 具体如何?你想对他们做什么?只是显示在屏幕上?放入变量?还有什么?
  • @Littlefoot 我更新了问题。

标签: database oracle plsql package plsql-package


【解决方案1】:

一种选择是使用游标 FOR 循环(为什么?因为客户可以有多个订单,并且 - 如果您使用单个 select 语句 - 您可能会收到 too_many_rows 错误,所以您d 必须选择到集合中,或者 - 就像我的示例一样 - 使用循环。

SQL> create or replace procedure getallorders2 (par_customer_id in customer.customerno%type)
  2  is
  3  begin
  4    for cur_r in (select c.customerno,
  5                         c.customername,
  6                         c.custbalance,
  7                         o.orderdate,
  8                         o.orderno
  9                  from customer c join customerorder o
 10                    on c.customerno = o.customerno
 11                  where c.customerno = par_customer_id
 12                 )
 13    loop
 14      dbms_output.put_line
 15        (cur_r.customerno   ||', '||
 16         cur_r.customername ||', '||
 17         cur_r.custbalance  ||', '||
 18         cur_r.orderno      ||', '||
 19         to_char(cur_r.orderdate, 'dd.mm.yyyy')
 20        );
 21    end loop;
 22  end;
 23  /

Procedure created.

测试:

SQL> set serveroutput on;
SQL> begin
  2    getallorders2('A101');
  3  end;
  4  /
A101, basant, 32000, O101, 04.02.2021

PL/SQL procedure successfully completed.

SQL>

现在您知道如何,将代码移动到包体中。

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多