当我 select 时,正文包 pl/sql 有问题

i have a issue in body package pl/sql when I select

我目前有一个程序存储在一个包中。我想从客户端 table 获取所有数据,但出现错误。问题出在存储的过程中 p_listar_cliente.

代码是:

创建 table 并插入

create table cliente (id NUMBER, name VARCHAR(50), address VARCHAR(50), salary NUMBER);

INSERT INTO cliente VALUES (1, 'camilo chaparro', 'popayan', 563321);
INSERT INTO cliente VALUES (2, 'ernesto lopez', 'ibague', 768954);
INSERT INTO cliente VALUES (3, 'camila españa', 'medellin', 113456);
INSERT INTO cliente VALUES (4, 'john jimenez', 'pasta', 789456);
INSERT INTO cliente VALUES (5, 'silvio jimenez', 'cali', 456123);

创建包:

CREATE OR REPLACE PACKAGE paquete_cliente_listar IS
--add clientes
  PROCEDURE p_agregar_clientes (
                                    c_id IN cliente.id%TYPE,
                                    c_name IN cliente.name%TYPE,
                                    c_address IN cliente.address%TYPE,
                                    c_salary IN cliente.salary%TYPE
                                    );

--delete clientes
  PROCEDURE p_quitar_clientes (c_id IN cliente.id%TYPE);

--Lists all customers 
   PROCEDURE p_listar_cliente;

--listar todos los clientes
END paquete_cliente_listar;

正文包:

CREATE OR REPLACE PACKAGE BODY paquete_cliente_listar AS 
--agregar clientes
  PROCEDURE p_agregar_clientes (
                                    c_id  cliente.id%TYPE,
                                    c_name  cliente.name%TYPE,
                                    c_address  cliente.address%TYPE,
                                    c_salary cliente.salary%TYPE
                                    ) IS
    BEGIN 

        INSERT INTO cliente (id, name, address, salary) VALUES (c_id, c_name, c_address, c_salary);

    END p_agregar_clientes;

--quitar clientes
  PROCEDURE p_quitar_clientes (c_id IN cliente.id%TYPE) IS

  BEGIN 

    DELETE FROM cliente WHERE id = c_id;


  END p_quitar_clientes;

--listar todos los clientes

 PROCEDURE p_listar_cliente AS
    BEGIN

    clientes_cursor cliente%rowtype;

     CURSOR clientes_cursor IS 
        SELECT id, name, address, salary FROM cliente;




    OPEN clientes_cursor;
    LOOP
      FETCH clientes_cursor INTO clientes_cursor;
      EXIT WHEN clientes_cursor%notfound;
      DBMS_OUTPUT.put_line(clientes_cursor.id || ' ' || clientes.name);
     END LOOP;

 END p_listar_cliente;


END paquete_cliente_listar;

我有这个问题:

错误:包体PAQUETE_CLIENTE_LISTAR Line/Col:30/21 PLS-00103:在期望以下之一时遇到符号 "CLIENTE":

:=。 ( @% ; 符号“:=”被替换为 "CLIENTE" 以继续。

Line/Col:32/13 PLS-00103:在期望以下之一时遇到符号 "CLIENTES_CURSOR":

:=。 ( @% ;

第一个问题是变量必须在BEGIN之前声明。但是,如果您使用游标 for 循环,而不是试图弄清楚变量是如何工作的,您的代码将更简单和更快,如下所示:

...
 PROCEDURE p_listar_cliente AS

 BEGIN

    FOR clientes IN
    (
      SELECT id, name, address, salary FROM cliente
    ) LOOP
      DBMS_OUTPUT.put_line(clientes.id || ' ' || clientes.name);
    END LOOP;

 END p_listar_cliente;
...

变量声明存在问题。必须在 BEGIN 之前。

PROCEDURE p_listar_cliente AS
clientes_cursor cliente%rowtype; -- moved it before BEGIN
 CURSOR clientes_cursor IS 
    SELECT id, name, address, salary FROM cliente;   -- moved it before BEGIN 

BEGIN
OPEN clientes_cursor;
LOOP
  FETCH clientes_cursor INTO clientes_cursor;
  EXIT WHEN clientes_cursor%notfound;
  DBMS_OUTPUT.put_line(clientes_cursor.id || ' ' || clientes.name);
 END LOOP;

 END p_listar_cliente;

使用后关闭游标也是一个好习惯。

干杯!!