使用输入参数并返回数据的函数创建时出现编译错误

时间:2018-12-03 12:45:25

标签: oracle function plsql

创建一个名为“ find_balance”的函数,该函数以invoice_id作为输入并返回类型为balance_status的{​​{1}}。
函数名称:varchar 输入参数:find_balance中的invoice_id
输出变量:int,数据类型为balance_status

设计规则:

  1. 如果给定发票ID的余额为零,则显示为“付款已完成”状态。
  2. 如果给定发票ID的余额大于零,则状态显示为“尚未付款”。我写了以下查询:

varchar

1 个答案:

答案 0 :(得分:0)

您已经很接近解决方案了;我想知道为什么您自己没有解决问题。

样本表和数据:

SQL> create table shipment_entity
  2    (id number,
  3     find_balance number
  4    );

Table created.

SQL> insert into shipment_entity values (1, 100);

1 row created.

SQL> insert into shipment_entity values (2, 0);

1 row created.

功能:我标记了您做错了(第5、12、18、22行):

SQL> CREATE OR REPLACE FUNCTION find_balance (invoice_id IN INTEGER)
  2     RETURN VARCHAR2
  3  IS
  4     c_find_balance   NUMBER (5, 2);
  5     balance_status  VARCHAR2 (255);         --> VARCHAR2, not VARCHAR
  6  BEGIN
  7     SELECT find_balance
  8       INTO c_find_balance
  9       FROM shipment_entity
 10      WHERE id = invoice_id;
 11
 12     IF c_find_balance = 0                   --> =, not ==
 13     THEN
 14        balance_status := 'The payment has been Completed';
 15     ELSE
 16        IF c_find_balance > 0
 17        THEN
 18           balance_status := c_find_balance || ' yet to be paid';  --> missing concatenation; wrong variable name
 19        END IF;
 20     END IF;
 21
 22     RETURN (balance_status);                --> balance_status, not c_find_balance
 23  END;
 24  /

Function created.

测试:

SQL> select find_balance(1) res1,
  2         find_balance(2) res2
  3  from dual;

RES1
--------------------------------------------------------------------------------
RES2
--------------------------------------------------------------------------------
100 yet to be paid
The payment has been Completed


SQL>