INSERT不起作用,在ORACLE中使用SQL PLUS,定义类型的简单表,完美创建但不插入值
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您定义了两个相同类型的构造函数,但您没有意识到您可能这样做了。
当您创建对象类型时,Oracle 会创建一个默认构造函数,其参数与该类型的参数相匹配。因此,当您还定义了一个仅以纬度和经度作为输入的构造函数时,Oracle 无法确定要调用哪个构造函数、默认创建的构造函数还是您的构造函数,因此会出现错误:
要解决此问题,您可以简化代码:
即,您不需要构造函数声明或主体。如果你愿意,你可以有一个不同的构造函数,例如:
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:
To fix this, you can simplify your code:
Ie, you don't need the constructor declaration or the body. If you like, you can have a DIFFERENT constructor, eg: