在中我介紹了如何通過定義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�