如何在Oracle中修改Collection類型的變數。

來源:互聯網
上載者:User

如何在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&#2208830

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.