不能在 select 语句中使用过程?
Cannot use procedure in the select statement?
下面是我创建类型和表的代码。全部正确:
CREATE TYPE shape_typ AS OBJECT (
l INTEGER, -- length
w INTEGER, -- width
h INTEGER, -- height
MEMBER FUNCTION area RETURN INTEGER,
MEMBER FUNCTION volume RETURN INTEGER,
MEMBER PROCEDURE display (SELF IN OUT NOCOPY shape_typ) );
CREATE TYPE BODY shape_typ AS
MEMBER FUNCTION volume RETURN INTEGER IS
BEGIN
RETURN l * w * h;
-- same as previous line RETURN SELF.l * SELF.w * SELF.h;
END;
MEMBER FUNCTION area RETURN INTEGER IS
BEGIN -- not necessary to include SELF in following
RETURN 2 * (l * w + l * h + w * h);
END;
MEMBER PROCEDURE display (SELF IN OUT NOCOPY shape_typ) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Length: ' || l || ' - ' ||
'Width: ' || w || ' - ' ||
'Height: ' || h );
DBMS_OUTPUT.PUT_LINE('Volume: ' || volume || ' - ' ||
'Area: ' || area );
END;
END;
CREATE TABLE shapes (
shape shape_typ,
create_date DATE );
INSERT INTO shapes VALUES (
shape_typ (3,3,3), '17-MAR-2008' );
INSERT INTO shapes VALUES (
shape_typ (1,8,2), '17-FEB-2008' );
INSERT INTO shapes VALUES (
shape_typ (1,1,1), '27-MAR-2008' );
SELECT s.shape.l,s.shape.w,s.shape.h,s.shape.area() FROM shapes s;
DECLARE
shap shapes%rowtype;
BEGIN -- PL/SQL block for selecting-displaying a student
SELECT * INTO shap
FROM shapes s
WHERE s.shape.l=1 and s.shape.w=1 and s.shape.h=1;
shap.shape.display;
END;
但是,当我尝试执行以下语句中的过程时,它给了我一条错误消息。
SELECT s.shape.l,s.shape.w,s.shape.h,s.shape.display() FROM shapes s;
错误信息是:
ORA-06553: PLS-222: no function with name 'DISPLAY' exists in this scope
06553. 00000 - "PLS-%s: %s"
*Cause:
*Action:
Error at Line: 49 Column: 38
不知是不是因为我不能在select语句中使用程序?
不,您不能在 SQL-语句中使用过程。
过程 DISPLAY
只能在 PL/SQL 上下文中使用。
参见 Oracle 文档:Coding PL/SQL Subprograms and Packages
下面是我创建类型和表的代码。全部正确:
CREATE TYPE shape_typ AS OBJECT (
l INTEGER, -- length
w INTEGER, -- width
h INTEGER, -- height
MEMBER FUNCTION area RETURN INTEGER,
MEMBER FUNCTION volume RETURN INTEGER,
MEMBER PROCEDURE display (SELF IN OUT NOCOPY shape_typ) );
CREATE TYPE BODY shape_typ AS
MEMBER FUNCTION volume RETURN INTEGER IS
BEGIN
RETURN l * w * h;
-- same as previous line RETURN SELF.l * SELF.w * SELF.h;
END;
MEMBER FUNCTION area RETURN INTEGER IS
BEGIN -- not necessary to include SELF in following
RETURN 2 * (l * w + l * h + w * h);
END;
MEMBER PROCEDURE display (SELF IN OUT NOCOPY shape_typ) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Length: ' || l || ' - ' ||
'Width: ' || w || ' - ' ||
'Height: ' || h );
DBMS_OUTPUT.PUT_LINE('Volume: ' || volume || ' - ' ||
'Area: ' || area );
END;
END;
CREATE TABLE shapes (
shape shape_typ,
create_date DATE );
INSERT INTO shapes VALUES (
shape_typ (3,3,3), '17-MAR-2008' );
INSERT INTO shapes VALUES (
shape_typ (1,8,2), '17-FEB-2008' );
INSERT INTO shapes VALUES (
shape_typ (1,1,1), '27-MAR-2008' );
SELECT s.shape.l,s.shape.w,s.shape.h,s.shape.area() FROM shapes s;
DECLARE
shap shapes%rowtype;
BEGIN -- PL/SQL block for selecting-displaying a student
SELECT * INTO shap
FROM shapes s
WHERE s.shape.l=1 and s.shape.w=1 and s.shape.h=1;
shap.shape.display;
END;
但是,当我尝试执行以下语句中的过程时,它给了我一条错误消息。
SELECT s.shape.l,s.shape.w,s.shape.h,s.shape.display() FROM shapes s;
错误信息是:
ORA-06553: PLS-222: no function with name 'DISPLAY' exists in this scope 06553. 00000 - "PLS-%s: %s" *Cause:
*Action: Error at Line: 49 Column: 38
不知是不是因为我不能在select语句中使用程序?
不,您不能在 SQL-语句中使用过程。
过程 DISPLAY
只能在 PL/SQL 上下文中使用。
参见 Oracle 文档:Coding PL/SQL Subprograms and Packages