我必须编写一个 pl/sql 代码(实际上是一个函数),当以郊区为参数时,它返回一个邮政编码,代码如下:
create or replace
FUNCTION get_postCode(p_suburb IN varchar2)
RETURN varchar2
IS
--
v_postcode varchar2;
--
CURSOR c1 IS
SELECT locality, postcode FROM table_postcode;
--
BEGIN
--
FOR r1 IN c1
loop
IF upper(r1.locality) = upper(p_suburb)
THEN
v_postcode := r1.postcode;
return v_postcode;
END IF;
exit WHEN c1%notfound;
END loop;
-- return v_postcode;
--
exception WHEN others then
v_postcode := null;
END;
table_postcode 是从 Post Office 获得的,它包含郊区(地区作为表格中的列)和邮政编码以及与此案例无关的其他字段。
当我使用该函数时,它返回正确的值,当我将这个函数用作 SELECT 子句的列时,它仅在我不在 FROM 子句之后添加任何其他子句时才返回。这对我来说很奇怪。
情况是:
select street, suburb, get_postcode(suburb) from my_table;
上面的行给了我结果,但是
select street, subur, get_postcode(suburb) from my_table order by suburb;
失败并给我以下错误消息:
ORA-06503: PL/SQL: Function returned without value
ORA-06512: at "U11254683.GET_POSTCODE", line 25
06503. 00000 - "PL/SQL: Function returned without value"
*Cause: A call to PL/SQL function completed, but no RETURN statement was
executed.
*Action: Rewrite PL/SQL function, making sure that it always returns
a value of a proper type.
如果我在一个块中调用该函数,例如:
Declare
v_post varchar2(10);
Begin
v_post := get_postcode('Sydney');
DBMS_OUTPUT.PUT_LINE('The post code is '||v_post);
End;
结果是正确的,给了我 2000。