简体   繁体   English

较低的 function 在 SQL Z30162ED78B6C10F74FCZF14Z 中用于 function 时被忽略

[英]Lower function being ignored when used in a function in SQL Oracle

I'm having trouble with creating a function in SQL Oracle that is case insensitive.我在 SQL Oracle 中创建不区分大小写的 function 时遇到问题。 I have been asked to create a function called我被要求创建一个名为 function

containsText(pString1, pString2)

to find if pString2 is in pString1.查找 pString2 是否在 pString1 中。 I'm using the lower() function but it seems to be ignoring the command.我正在使用 lower() function 但它似乎忽略了命令。 If anyone would like to have a go here are the scripts.如果有人想要 go,这里是脚本。

First create tables:首先创建表:

-- create types --
create or replace type artist_type as object 
(artistName     varchar(50), 
 artistRole     varchar(25))
/ 
create type artist_array_type  
as varray(5) of artist_type
/ 
create or replace type review_type as object 
(reviewerName   varchar(25), 
 reviewDate     date,
 reviewText     varchar(250), 
 reviewScore    number)
/
create or replace type review_table_type as table of review_type
/
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),
 albumArtists       artist_array_type,
 albumReviews       review_table_type,
member function discountPrice return number,
member function containsText (pString1 varchar2, pString2 varchar2) 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 tables --
create table albums of album_type 
object id system generated
nested table albumReviews store as store_reviews 
/ 

Here is one row of data:这是一行数据:

insert into albums 
values (disk_type('The Essential Bob Dylan', 99, '8-Jul-2016', 'Pop', 37.00, 32, 
artist_array_type(artist_type('Bob Dylan', 'Composer'), 
                  artist_type('Bob Dylan', 'Vocals')), 
review_table_type(review_type('Shawn', '24-Jul-2018', 'Wife loved it!', 5), 
                  review_type('Reuben', '2-Aug-2019', 'Great compilation of some of his most known songs', 5)),
'Vinyl', 2, NULL, 11));

Here is the function:这是 function:

create or replace type body album_type
as 
member function discountPrice return number is

    begin 
        return null;
    end discountPrice;

    member function containsText (pString1 varchar2, pString2 varchar2) return integer is

    begin
        if lower(pString1) like lower('%' || pString2 || '%') then 
             return 1;
        else
            return 0;
        end if;
    end containsText;
end;

Finally the select statement:最后是 select 语句:

select containsText('This is great', 'Great') from albums

should return 1 but returns 0.应该返回 1 但返回 0。

Additionally, I have tried the following alternative if statement.此外,我尝试了以下替代 if 语句。

   begin
        if instr(lower(pString1), lower(pString2)) > 0 then 
             return 1;
        else
            return 0;
        end if;
    end containsText;
end;

Your code is not calling the member function, it's calling a different standalone function.您的代码没有调用成员 function,而是调用了不同的独立 function。 Use code like this to call the member function owned by the type:使用如下代码调用类型拥有的成员 function:

select a.containsText('This is great', 'Great') result
from albums a;

RESULT
------
     1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM