在oracle plsql中创建成员函数
Creating member functions in oracle plsql
我对 oracle 和 plsql 很陌生,我通常用 python 写。我需要在子类型下创建成员函数,但不明白错误 PLS-00538 和 PLS-00539 指的是什么。这是我的代码:
create or replace type album_type as object
(albumTitle varchar(50),
albumPlaytime number(3), -- minutes
albumReleaseDate date,
albumGenre varchar(15),
albumPrice number(9,2),
albumTracks number(2),
member function discountPrice return number)
return integer)
not instantiable not final
/
create or replace type disk_type under album_type
( mediaType varchar(10),
diskNum number(2), -- number of disks
diskUsedPrice number(9,2),
diskDeliveryCost number(9,2),
overriding member function discountPrice return number)
/
create or replace type mp3_type under album_type
(downloadSize number, -- size in MB
overriding member function discountPrice return number)
/
目前我正在尝试编译正文类型以尝试调试我的错误。下面的代码有效但不是正确的:
create or replace type body album_type as
member function discountPrice return number is
begin
return albumprice;
end discountPrice;
end;
/
但是,在子类型上应用相同的方法时会出现错误:
1) PLS-00539: subprogram 'DISCOUNTPRICE' 在对象类型主体中声明,必须在对象类型规范中定义。
2) PLS-00538: subprogram or cursor 'DISCOUNTPRICE' is declared in an object type specification and must be defined in the object type body
create or replace type body disk_type as
member function discountPrice return number is
discount number;
begin
discount:= round(albumprice - albumprice*0.20,0);
return discount;
end discountPrice;
end;
/
最终,我希望该功能能够提供 20% 的折扣。逻辑上应该是:
if mediaType == 'Audio_CD':
price = albumprice - albumprice*0.20
类型主体中的函数声明必须完全匹配类型规范中的声明。在这种情况下,缺少关键字 OVERRIDING
。
create or replace type body disk_type as
overriding member function discountPrice return number is
discount number;
begin
discount:= round(albumprice - albumprice*0.20,0);
return discount;
end discountPrice;
end;
/
此外,代码在 return integer)
附近有一个错误,但我猜那只是一个错字。
我对 oracle 和 plsql 很陌生,我通常用 python 写。我需要在子类型下创建成员函数,但不明白错误 PLS-00538 和 PLS-00539 指的是什么。这是我的代码:
create or replace type album_type as object
(albumTitle varchar(50),
albumPlaytime number(3), -- minutes
albumReleaseDate date,
albumGenre varchar(15),
albumPrice number(9,2),
albumTracks number(2),
member function discountPrice return number)
return integer)
not instantiable not final
/
create or replace type disk_type under album_type
( mediaType varchar(10),
diskNum number(2), -- number of disks
diskUsedPrice number(9,2),
diskDeliveryCost number(9,2),
overriding member function discountPrice return number)
/
create or replace type mp3_type under album_type
(downloadSize number, -- size in MB
overriding member function discountPrice return number)
/
目前我正在尝试编译正文类型以尝试调试我的错误。下面的代码有效但不是正确的:
create or replace type body album_type as
member function discountPrice return number is
begin
return albumprice;
end discountPrice;
end;
/
但是,在子类型上应用相同的方法时会出现错误: 1) PLS-00539: subprogram 'DISCOUNTPRICE' 在对象类型主体中声明,必须在对象类型规范中定义。 2) PLS-00538: subprogram or cursor 'DISCOUNTPRICE' is declared in an object type specification and must be defined in the object type body
create or replace type body disk_type as
member function discountPrice return number is
discount number;
begin
discount:= round(albumprice - albumprice*0.20,0);
return discount;
end discountPrice;
end;
/
最终,我希望该功能能够提供 20% 的折扣。逻辑上应该是:
if mediaType == 'Audio_CD':
price = albumprice - albumprice*0.20
类型主体中的函数声明必须完全匹配类型规范中的声明。在这种情况下,缺少关键字 OVERRIDING
。
create or replace type body disk_type as
overriding member function discountPrice return number is
discount number;
begin
discount:= round(albumprice - albumprice*0.20,0);
return discount;
end discountPrice;
end;
/
此外,代码在 return integer)
附近有一个错误,但我猜那只是一个错字。