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

发布于 2024-12-22 00:30:25 字数 758 浏览 4 评论 0原文

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');
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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-12-29 00:30:25

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

当您创建对象类型时,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');

Your problem is that you have defined two constructors of the same type, but you didn't realize you did probably.

When you create an object type, Oracle creates a default constructor, with parameters that match the parameters of the type. Therefore, when you also defined a constructor with only lattitude and longitude as inputs, Oracle cannot work out which constructor to call, the default created one, or your one, so it errors with:

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

To fix this, you can simplify your code:

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');

Ie, you don't need the constructor declaration or the body. If you like, you can have a DIFFERENT constructor, eg:

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文