pl/sql函数,用于查找符合贷款条件的客户和贷款金额

sxpgvts3  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(472)

注:问题有两个表:客户(身份证、姓名、部门、职务、工资)和账户(身份证、账号、定期存款)。我为此编写了pl/sql函数:

CREATE OR REPLACE FUNCTION loan_func (sal IN NUMBER,fd in NUMBER)
RETURN NUMBER 
IS 
loan NUMBER(6);
BEGIN
IF (sal>60000 AND fd>100000) THEN
loan := (sal*0.4)+100000;
ELSIF (sal>50000 AND sal<59999 AND fd>50000) THEN
loan := (sal*0.3)+90000;
END IF;
RETURN loan;
END;

中的函数输入正确,但光标不起作用!你能帮我解决这个问题得到答案吗?
下面是光标:

DECLARE
CURSOR CR IS
SELECT
    C_ID,
    A_ID,
    NAME,
    FIXED_DEPOSIT,
    SALARY
FROM
    CUSTOMER 
join ACCOUNT
on     C_ID = A_ID;
I CUSTOMER.C_ID%TYPE;
N CUSTOMER.NAME%TYPE;
fd ACCOUNT.FIXED_DEPOSIT%TYPE;
sal CUSTOMER.SALARY%TYPE;
Loan NUMBER(6);
BEGIN
OPEN CR;
LOOP
EXIT WHEN CR%NOTFOUND;
FETCH CR INTO I,N,fd,sal;
DBMS_OUTPUT.PUTLINE(I || N);
Loan:=loan_func(sal,fd); 
DBMS_OUTPUT.PUTLINE(loan);
END LOOP;
END;

它显示以下错误:
ora-06550:第22行第2列:pls-00394:fetch语句的into列表中的值数目错误

pvcm50d1

pvcm50d11#

您最初的查询是正确的。它使用了ansi连接语法,这是首选语法。更正恢复为过时的(自1992年sql标准以来),但仍然支持语法。on子句提供了与missing语句相同的函数。
您的错误在游标中不是必需的,而是在获取中。游标包含5列,但是fetch只获取4个变量。在游标中不需要c\u id,或者需要将其添加到获取中。更好的方法是切换到cursor for循环,oracle负责处理游标。

declare
   loan number(6);
begin
   for cr in 
       ( select a_id, name,fixed_deposit fd, salary sal
           from customer c
           join account  a
             on c_id = a_id 
       ) 
   loop
      dbms_output.put ('id ='|| cr.a_id || 'name = ' ||cr.name n );
      loan:=loan_func(cr.sal,cr.fd); 
      dbms_output.putline(' loan amount is =' || loan);
   end loop
end;

我还更正了dbms\u输出语句。

wrrgggsh

wrrgggsh2#

DECLARE
CURSOR CR IS
SELECT
    ID,NAME,FIXED_DEPOSIT,SALARY
FROM
    CUSTOMER C,
    ACCOUNT A
WHERE
    C.ID=A.ID;  <============================ missing ';'

I C.ID%TYPE;
N C.NAME%TYPE;
fd A.FIXED_DEPOSIT%TYPE;
sal C.SALARY%TYPE;
Loan NUMBER(6);
BEGIN
    OPEN CR;
LOOP
EXIT WHEN CR%NOTFOUND
FETCH CR INTO I,N,fd,sal;
DBMS_OUTPUT.PUTLINE(‘ID = ||I||’ ‘NAME = ||N||’);
Loan:=loan_func(sal,fd); 
DBMS_OUTPUT.PUTLINE(‘LOAN AMOUNT IS =’|| loan);
END LOOP
END;

相关问题