INSERT不工作,使用ORACLE中的SQL PLUS,定义类型的简单表,完美创建但不插入值

时间:2011-12-16 12:03:55

标签: oracle oracle11g sqlplus

DROP TYPE Position;

CREATE OR REPLACE TYPE 
Position AS OBJECT
(longitude NUMBER(11,7),
lattitude NUMBER(11,7),
CONSTRUCTOR FUNCTION Position(
    long NUMBER,
    latt NUMBER
    ) RETURN SELF AS RESULT
)FINAL;
/


CREATE OR REPLACE TYPE BODY Position AS
CONSTRUCTOR FUNCTION Position(
    long NUMBER,
    latt NUMBER
    ) RETURN SELF AS RESULT IS
  BEGIN
      SELF.longitude := long;
      SELF.lattitude := latt;
  RETURN;
  END;

END;
/

DESC Position;


DROP TABLE District_Info;

CREATE TABLE District_Info(
Dname VARCHAR2(20),
DPos Position,
Boundary_dist VARCHAR2(20),
Launch_ghat CHAR(1)
);

DESC District_Info;


INSERT INTO District_Info (Dname,DPos,Boundary_dist,Launch_ghat)
VALUES ('d',Position(1.1, 1.1),'gr','y');

1 个答案:

答案 0 :(得分:3)

你的问题是你已经定义了两个相同类型的构造函数,但你没有意识到你可能做到了。

创建对象类型时,Oracle会创建一个默认构造函数,其参数与该类型的参数相匹配。因此,当您还定义了仅使用经度和经度作为输入的构造函数时,Oracle无法确定要调用的构造函数,默认创建的构造函数或您的构造函数,因此它出错:

SQL Error: ORA-06553: PLS-307: too many declarations of 'POSITION' match this call

要解决此问题,您可以简化代码:

drop type position;

CREATE OR REPLACE TYPE Position AS OBJECT (longitude NUMBER(11,7), lattitude NUMBER(11,7))
/

DROP TABLE District_Info;

CREATE TABLE District_Info( Dname VARCHAR2(20), DPos Position, Boundary_dist VARCHAR2(20), Launch_ghat CHAR(1) );


INSERT INTO District_Info (Dname,DPos,Boundary_dist,Launch_ghat) VALUES ('d',Position(1.1),'gr','y');

即,您不需要构造函数声明或正文。如果您愿意,可以使用不同的构造函数,例如:

CREATE OR REPLACE TYPE Position AS OBJECT (longitude NUMBER(11,7), lattitude NUMBER(11,7),
-- Define a constructor that has only 2 parameters.
  CONSTRUCTOR FUNCTION position(i_longitude NUMBER)
    RETURN SELF AS RESULT
)
/

CREATE OR REPLACE TYPE BODY Position AS 
  CONSTRUCTOR FUNCTION position(i_longitude NUMBER)
    RETURN SELF AS RESULT IS 
  BEGIN 
    SELF.longitude := i_longitude; 
    SELF.lattitude := i_longitude; 
    RETURN; -- self; 
  END;
END; 
/

show errors;

DROP TABLE District_Info;

CREATE TABLE District_Info( Dname VARCHAR2(20), DPos Position, Boundary_dist VARCHAR2(20), Launch_ghat CHAR(1) );

DESC District_Info;

INSERT INTO District_Info (Dname,DPos,Boundary_dist,Launch_ghat) VALUES ('d',Position(1.1),'gr','y');