在如何在JAVA程式中使用Struct一次傳入多條資料給Oracle的預存程序中我介紹了如何通過定義Struct和Array在JAVA程式中一次傳入多條資料給Oracle的預存程序。
步驟一:定義物件類型。
CREATE TYPE department_type AS OBJECT (
DNO NUMBER (10),
NAME VARCHAR2 (50),
LOCATION VARCHAR2 (50)
);
步驟二:定義一個物件類型的數組對象。
CREATE TYPE dept_array AS TABLE OF department_type;
步驟三:定義預存程序來插入資料。
CREATE OR REPLACE PACKAGE objecttype AS
PROCEDURE insert_object (d dept_array);
END objecttype;
CREATE OR REPLACE PACKAGE BODY objecttype
AS
PROCEDURE insert_object (d dept_array)
AS
BEGIN
FOR i IN d.FIRST..d.LAST
LOOP
INSERT INTO department_teststruct
VALUES (d(i).dno,d(i).name,d(i).location);
END LOOP;
END insert_object;
END objecttype;
如果我們需要對dept_array類型的d進行的更新的話,那麼直接使用下面的語句系統會提示錯誤。
CREATE OR REPLACE PACKAGE objecttype AS
PROCEDURE insert_object (d dept_array);
END objecttype;
CREATE OR REPLACE PACKAGE BODY objecttype
AS
PROCEDURE insert_object (d dept_array)
AS
BEGIN
FOR i IN d.FIRST..d.LAST
LOOP
d(i).location := 'New Loc'||i;
INSERT INTO department_teststruct
VALUES (d(i).dno,d(i).name,d(i).location);
END LOOP;
END insert_object;
END objecttype;
錯誤提示: PLS-00363: expression 'D.LOCATION' cannot be used as an assignment target
正確的方法是:
CREATE OR REPLACE PACKAGE BODY objecttype
AS
procedure insert_object(d
in
out dept_array)
is
begin
for i in 1..d.count loop
--與FOR i IN d.FIRST..d.LAST 功能相同 d(i).location := 'New Loc'||i;
INSERT INTO department_teststruct
VALUES (d (i).dno,d (i).name,d (i).location);
end loop;
end insert_object;
END objecttype;
關鍵問題是:d 必須是output類型(代碼中紅色粗體標記部分)。
參考內容:http://forums.oracle.com/forums/thread.jspa?messageID=2208830�