Oracle 11 g PL/SQL这是一个家庭作业[已关闭]

yqhsw0fo  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(149)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
2天前关闭。
Improve this question
我需要修改我的包。第一步是添加一个名为DD_PLIST_PP的过程,该过程显示捐赠者名称和所有相关的认捐(认捐ID、首次付款到期日和最后付款到期日。捐赠者ID是该过程的输入值。第二步是将该过程设置为公共,将两个函数设置为私有,并使用匿名块测试该过程。这在Oracle 11g: PL/SQL programming 2E by JOAN CASTEEL
下面是我创建的代码,我所期望的是

CREATE OR REPLACE PACKAGE pledge_pkg AS
 PROCEDURE DD_PLIST_PP(p_donor_id IN dd_donor.iddonor%TYPE);
END pledge_pkg;
/

CREATE OR REPLACE PACKAGE BODY pledge_pkg AS
 FUNCTION dd_paydate1_sf(p_id IN dd_pledge.idpledge%TYPE) RETURN DATE IS
  lv_pl_dat DATE;
  lv_mth_txt VARCHAR2(2);
  lv_yr_txt VARCHAR2(4);
BEGIN
  SELECT ADD_MONTHS(pledgedate, 1)
  INTO lv_pl_dat
  FROM dd_pledge
  WHERE idpledge = p_id;
  lv_mth_txt := TO_CHAR(lv_pl_dat, 'mm');
  lv_yr_txt := TO_CHAR(lv_pl_dat, 'yyyy');
  RETURN TO_DATE((lv_mth_txt || '-01-' || lv_yr_txt), 'mm-dd-yyyy');
END;

FUNCTION dd_payend_sf(p_id IN dd_pledge.idpledge%TYPE) RETURN DATE IS
  lv_pay1_dat DATE;
  lv_mths_num dd_pledge.paymonths%TYPE;
BEGIN
  SELECT dd_paydate1_sf(idpledge), paymonths - 1
  INTO lv_pay1_dat, lv_mths_num
  FROM dd_pledge
  WHERE idpledge = p_id;
 IF lv_mths_num = 0 THEN
   RETURN lv_pay1_dat;
 ELSE
   RETURN ADD_MONTHS(lv_pay1_dat, lv_mths_num);
 END IF;
END;

PROCEDURE DD_PLIST_PP(p_donor_id IN dd_donor.iddonor%TYPE) IS
 v_donor_name dd_donor.firstname%TYPE;
 v_pledge_id dd_pledge.idpledge%TYPE;
 v_date1 DATE;
 v_date2 DATE;
BEGIN
-- Get donor name
SELECT firstname
INTO v_donor_name
FROM dd_donor
WHERE iddonor = p_donor_id;

DBMS_OUTPUT.PUT_LINE('Donor Name: ' || v_donor_name);
DBMS_OUTPUT.PUT_LINE('Pledge ID | First Payment Due Date | Last Payment Due Date');

-- Get associated pledges and print details
FOR pledge_rec IN (SELECT idpledge FROM dd_pledge WHERE iddonor = p_donor_id) LOOP
  v_pledge_id := pledge_rec.idpledge;
  v_date1 := dd_paydate1_sf(v_pledge_id);
  v_date2 := dd_payend_sf(v_pledge_id);

  DBMS_OUTPUT.PUT_LINE(
    v_pledge_id || ' | ' ||
    TO_CHAR(v_date1, 'DD-MON-YYYY') || ' | ' ||
    TO_CHAR(v_date2, 'DD-MON-YYYY')
  );
  END LOOP;
END;
END pledge_pkg;

/
DECLARE
 v_donor_id dd_donor.iddonor%TYPE := 301; -- Replace with the desired donor ID
BEGIN
 -- Call the public procedure to display donor and associated pledges
 pledge_pkg.DD_PLIST_PP(v_donor_id);
END;

字符串
This is the expected output
所期望的是上图中的输出。

ffscu2ro

ffscu2ro1#

您的代码给出错误:
PLS-00231:SQL中不能使用函数“DD_PAYDATE1_SF”
如果你将函数设为私有,那么它们就不能在SQL中使用(只能在包内的PL/SQL中使用)。
你可能不应该在SQL中调用这些函数,因为你将从SQL上下文切换到PL/SQL再返回到SQL来查询同一个表并找到相同的行信息,而你本可以在SQL中从原始函数计算它。
你可能应该像这样实现它:

CREATE OR REPLACE PACKAGE BODY pledge_pkg AS
 FUNCTION dd_paydate1_sf(p_id IN dd_pledge.idpledge%TYPE) RETURN DATE IS
  lv_pl_dat DATE;
BEGIN
  SELECT TRUNC(ADD_MONTHS(pledgedate, 1), 'MM')
  INTO lv_pl_dat
  FROM dd_pledge
  WHERE idpledge = p_id;

  RETURN lv_pl_dat;
END;

FUNCTION dd_payend_sf(p_id IN dd_pledge.idpledge%TYPE) RETURN DATE IS
  lv_pay1_dat DATE;
  lv_mths_num dd_pledge.paymonths%TYPE;
BEGIN
  SELECT ADD_MONTHS(TRUNC(pledgedate, 'MM'), paymonths)
  INTO lv_pay1_dat
  FROM dd_pledge
  WHERE idpledge = p_id;
 
  RETURN lv_pay1_dat;
END;

PROCEDURE DD_PLIST_PP(p_donor_id IN dd_donor.iddonor%TYPE) IS
 v_donor_name dd_donor.firstname%TYPE;
 v_pledge_id dd_pledge.idpledge%TYPE;
 v_date1 DATE;
 v_date2 DATE;
BEGIN
-- Get donor name
SELECT firstname
INTO v_donor_name
FROM dd_donor
WHERE iddonor = p_donor_id;

DBMS_OUTPUT.PUT_LINE('Donor Name: ' || v_donor_name);
DBMS_OUTPUT.PUT_LINE('Pledge ID | First Payment Due Date | Last Payment Due Date');

-- Get associated pledges and print details
FOR pledge_rec IN (
  SELECT idpledge,
         ADD_MONTHS(TRUNC(pledgedate, 'MM'), 1) AS pledge_start,
         ADD_MONTHS(TRUNC(pledgedate, 'MM'), paymonths) AS pledge_end
  FROM   dd_pledge
  WHERE  iddonor = p_donor_id
) LOOP
  DBMS_OUTPUT.PUT_LINE(
    pledge_rec.idpledge
    || ' | '
    || TO_CHAR(pledge_rec.pledge_start, 'DD-MON-YYYY')
    || ' | '
    || TO_CHAR(pledge_rec.pledge_end, 'DD-MON-YYYY')
  );
  END LOOP;
END;
END pledge_pkg;
/

字符串
然后输出为:

Donor Name: Alice
Pledge ID | First Payment Due Date | Last Payment Due Date
1 | 01-FEB-2023 | 01-JAN-2024


fiddle

相关问题