Oracle
 sql >> Database >  >> RDS >> Oracle

È possibile chiamare un tipo di record all'interno di un cursore?

Devi dichiarare il tipo di record prima di usarlo nel cursore, quindi dichiarare una variabile per il record da passare nel cursore e quindi scorrere il cursore.

DECLARE
  TYPE dtype_amount IS RECORD (
    typecode DD_DONOR.TYPECODE%TYPE,
    amount   DD_PLEDGE.PLEDGEAMT%TYPE
  );

  CURSOR donnor_pledge (p_typeamount dtype_amount ) IS
    SELECT d.FIRSTNAME,
           d.LASTNAME,
           p.PLEDGEAMT
    FROM   DD_DONOR d
           INNER JOIN DD_PLEDGE p
           USING (IDDONOR)
    WHERE d.TYPECODE  = p_typeamount.typecode
    AND   p.PLEDGEAMT > p_typeamount.amount;

  p_typeamount dtype_amount;
  p_name_amount donnor_pledge%ROWTYPE;
BEGIN
  p_typeamount.typecode := 'I';
  p_typeamount.amount   := 250;
  OPEN donnor_pledge(p_typeamount);
  LOOP
    FETCH donnor_pledge INTO p_name_amount;
    EXIT WHEN donnor_pledge%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('Donor name: ' || p_name_amount.FIRSTNAME || ' ' || p_name_amount.LASTNAME);
    DBMS_OUTPUT.PUT_LINE('Pledge amount: ' || p_name_amount.PLEDGEAMT);
  END LOOP;
  CLOSE donnor_pledge;    
END;
/

oppure puoi farlo senza il record usando due parametri per il cursore:

DECLARE
  CURSOR donnor_pledge (p_typecode CHAR, p_amount NUMBER ) IS
    SELECT d.FIRSTNAME,
           d.LASTNAME,
           p.PLEDGEAMT
    FROM   DD_DONOR d
           INNER JOIN DD_PLEDGE p
           USING (IDDONOR)
    WHERE d.TYPECODE  = p_typecode
    AND   p.PLEDGEAMT > p_amount;
  
  p_name_amount donnor_pledge%ROWTYPE;
BEGIN
  OPEN donnor_pledge('I', 250);
  LOOP
    FETCH donnor_pledge INTO p_name_amount;
    EXIT WHEN donnor_pledge%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('Donor name: ' || p_name_amount.FIRSTNAME || ' ' || p_name_amount.LASTNAME);
    DBMS_OUTPUT.PUT_LINE('Pledge amount: ' || p_name_amount.PLEDGEAMT);
  END LOOP;
  CLOSE donnor_pledge;    
END;
/

db<>violino qui