遇到Oracle对象和方法的一些问题

时间:2017-04-15 17:42:36

标签: oracle

您好我是Oracle新手,我在编译时遇到了一些错误:

1 ORA-06545:PL / SQL:编译错误 - 编译中止 ORA-06550:第13行第1列: PLS-00103:遇到符号“CREATE” ORA-06550:第0行,第0列: PLS-00565:PERSON必须作为潜在的REF目标(对象类型)完成C:\ Users \ GV \ Documents \ School \ ex3.sql 6 1

我要做的是创建一个包含他的个人数据(姓名,地址,电话号码,电子邮件)的人员,以及检测电话号码是否仅为数字的2个方法,以及检测是否为电子邮件字符串中有一个@。

   create or replace type Person as object (
  --variables
  name varchar(10),
  address varchar(50),

  --methods
  member function pNum (phone number) return self as result,
  member function m (mail varchar) return self as result
) not final;

--body of methods

create or replace 
type Persona as

member function pNum (phone number) return self as result as
  begin
  if( phone is not number ) then
       RAISE_APPLICATION_ERROR( -20122,'phone can only have numbers' );
  end;

member function m (mail varchar) return self as result as
  begin
  if( mail contains('@') ) then
   RAISE_APPLICATION_ERROR( -20122,'not a email' );
  end;

所以我的问题是

为什么我在创建时遇到错误?

我的方法是否正确?

:::::::::::::::::: [UPDATE] :::::::::::::::::::::::::: :::::::::::::::::::::::::::

我已设法修复所有错误,但现在我需要一个if语句来检查电话号码中是否只有数字,而在电子邮件中有一个@ in string

更新的代码:

---------------------CLASS-----------------------------
create or replace type Person as object (
    --variables
    name varchar(10),
    address varchar(50),

    --methods
    member function pNum (phone number) return varchar2,
    member function ml (mail varchar) return varchar2
) not final;
/



-----------------------BODY----------------------------------
create or replace type BODY Person as
    member function pNum (phone number) return varchar2 as
      begin
        --if statement that detects if var phone is a number goes here
        --if( phone is not number ) then 
            return 'phone can only have numbers';
      end pNum;

    member function ml (mail varchar) return varchar2 as
      begin
        --if statement that detects if var mail has a @ character goes here
        --if( mail contains('@') ) then
            return 'not a email';
      end ml;
end;
/


--------------RUN TEST----------------------------------
DECLARE
  a Person;
begin
  a := Person(1,2);
  dbms_output.put_line('Phone: ' || a.pNum(1152795927));
end;

2 个答案:

答案 0 :(得分:0)

你必须坚持正确的语法。首先,您定义了正确的类型function updateIndexHTML() { this.plugin("done", function(stats) { var buildOutput = stats.toJson()['assetsByChunkName']['main'], outputFileName = buildOutput[0], currentIndexHTML = fs.readFileSync( path.join(__dirname, 'src', 'index.html'), { encoding: 'utf8' } ), $ = cheerio.load(currentIndexHTML); $('body').append(`<script src="build/${outputFileName}"></script>`); fs.writeFileSync( path.join(process.env.IONIC_WWW_DIR, 'index.html'), $.html() ); }); }

然后你尝试创建它的定义,这里是你弄错的地方。您必须将其定义为具有相同名称的BODY类型。您定义了Person并尝试为Persona创建正文:

Person

指定正文:

create or replace type Person as object (
    --variables
    name varchar(10),
    address varchar(50),

    --methods
    member function pNum (phone number) return self as result,
    member function m (mail varchar) return self as result
 ) not final;
 /

基本上你以错误的方式定义了身体。我提供的代码可能有语法错误,因为我在这里没有oracle来测试它。如果你发现任何问题,请告诉我,我会解决它。

编辑:函数必须返回一个类型。您无法返回create or replace type BODY Person as member function pNum (phone number) return number as begin if( phone is not number ) then RAISE_APPLICATION_ERROR( -20122,'phone can only have numbers' ); end if; return phone; end pNum; member function m (mail varchar) return varchar as begin if( mail contains('@') ) then RAISE_APPLICATION_ERROR( -20122,'not a email' ); end if; return mail; end m; end; / ,因为它是参数的定义。请在此处查看相关文档:SELF Parameters in Member Methods

答案 1 :(得分:0)

所以我设法让它发挥作用!

以下代码是包含个人数据(例如:姓名,地址,电话号码,电子邮件)和2种方法的类别,用于检测电话号码是否仅为数字,还有一种用于检测是否存在@在电子邮件字符串中。

已解决的代码:

---------------------Class Person-----------------------------
create or replace type Person as object (
    --variables
    name varchar(10),
    address varchar(50),

    --methods
    member function pNum (phone number) return varchar2,
    member function ml (mail varchar) return varchar2
) not final;
/



-----------------------Body of Methods----------------------------------
create or replace type BODY Person as
    member function pNum (phone number) return varchar2 as
      begin
        if(mod(phone,1) != 0) then
            return 'Phone number can only contain numbers';
        end if;
          return phone;
      end pNum;

    member function ml (mail varchar) return varchar2 as
      begin
        if (INSTR(mail, '@') != 0) then
            return mail;
        end if;
            return 'That is not a email';
      end ml;
end;
/


--------------Run Test----------------------------------
DECLARE
  a Person;
begin
  a := Person(1,2);
  dbms_output.put_line('Phone: ' || a.pNum(1152.795927));
  dbms_output.put_line('Email: ' || a.ml('armando@email.com'));

end;