使用编译错误创建的函数

时间:2018-01-07 14:25:59

标签: oracle plsql

我需要 创建一个接受输入参数k_city_id(数据类型number)的函数,并从表stadium_name返回指定城市的相应k_venue。如果某个城市中有多个场所,那么它应该引发too_namy_rows例外,其中包含'此城市有多个场所'的文字。

这是我的功能:

create or replace function find_venuee(k_city_id in number)
    return k_venue.stadium_name%type
is
    venue_name k_venue.stadium_name%type;
begin
    select stadium_name into venue_name from k_venue
    where city_id = k_city_id;

    return venue_name;
exception
    when too_many_rows then
        venue_name := 'This City has more than one venue'; 
        return venue_name;
end;
/

它正在创建具有编译错误的函数。代码有什么问题?

2 个答案:

答案 0 :(得分:1)

我们不知道那张桌子是怎么样的(所以我创造了自己的);一切似乎都没问题(见下面的例子)。

您是否可以发布类似的内容,以便我们可以看到您做了什么以及Oracle如何回应?

SQL> create table k_venue (city_id number, stadium_name varchar2(50));

Table created.

SQL>
SQL> create or replace function find_venuee (k_city_id in number)
  2    return k_venue.stadium_name%type
  3  is
  4    venue_name k_venue.stadium_name%type;
  5  begin
  6    select stadium_name
  7      into venue_name
  8      from k_venue
  9      where city_id = k_city_id;
 10
 11    return venue_name;
 12
 13  exception
 14    when too_many_rows then
 15      venue_name := 'This City has more than one venue';
 16      return venue_name;
 17  end;
 18  /

Function created.

SQL>
SQL> insert into k_venue values (1, 'Stadium A');

1 row created.

SQL>
SQL> select find_Venuee(1) From dual;

FIND_VENUEE(1)
--------------------------------------------------------------------------------
Stadium A

SQL>
SQL> insert into k_venue values (1, 'Stadium B');

1 row created.

SQL>
SQL> select find_Venuee(1) From dual;

FIND_VENUEE(1)
--------------------------------------------------------------------------------
This City has more than one venue

SQL>

答案 1 :(得分:0)

您不能拥有功能的动态返回类型。您必须创建一个类型为oracle pl / sql对象,然后使用它或右键“Varchar2”,因为您所说的函数将根据收到的城市ID返回场地名称。

请找到以下代码:

create or replace function find_venuee(k_city_id in number)
return varchar2 is
venue_name k_venue.stadium_name%type;
begin
select stadium_name into venue_name from k_venue
where city_id = k_city_id;
return venue_name;
exception
when too_many_rows then
venue_name := 'This City has more than one venue'; 
return venue_name;
end;
/

希望这会有所帮助。

由于 ANKIT。