创建一个名为“ find_balance
”的函数,该函数以invoice_id
作为输入并返回类型为balance_status
的{{1}}。
函数名称:varchar
输入参数:find_balance
中的invoice_id
输出变量:int
,数据类型为balance_status
设计规则:
varchar
答案 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>