【筆記】9i 文件中的一個問題

yellowlee發表於2009-03-31

9i pl/sql reference上有一個例子,querying data into collections of records,但是卻有幾處錯誤。jackywood告訴說:index by 用法有誤,用了index by 後面的

num_tab  NumTabTyp := NumTabTyp(2, 5, 8, 9);
  char_tab CharTabTyp := CharTabTyp('Tim', 'Jon', 'Beth', 'Jenny');
初始化會報錯。修改後,仍然報錯,提示

UPDATE tab1 SET (col1, col2) = rec_tab(i) WHERE col1 < 8;
update ...set 表示式必須是子查詢,百思不得其解,按照oracle文件說法,這樣update應該是可以被接受的。。

一下是相關的程式碼:

DECLARE
  /*
  create TABLE tab1 (col1 NUMBER, col2 VARCHAR2(20));
 
  CREATE TABLE tab2 (col1 NUMBER, col2 VARCHAR2(20));
 
  */
  TYPE RecTabTyp IS TABLE OF tab1%ROWTYPE INDEX BY BINARY_INTEGER;
  TYPE NumTabTyp IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
  TYPE CharTabTyp IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
  CURSOR c1 IS
    SELECT col1, col2 FROM tab2;
  rec_tab  RecTabTyp;
  num_tab  NumTabTyp := NumTabTyp(2, 5, 8, 9);
  char_tab CharTabTyp := CharTabTyp('Tim', 'Jon', 'Beth', 'Jenny');
BEGIN
  FORALL i IN 1 .. 4
    INSERT INTO tab1 VALUES (num_tab(i), char_tab(i));
  SELECT col1, col2 BULK COLLECT INTO rec_tab FROM tab1 WHERE col1 < 9;
  FORALL i IN rec_tab.FIRST .. rec_tab.LAST
    INSERT INTO tab2 VALUES rec_tab (i);
  FOR i IN rec_tab.FIRST .. rec_tab.LAST LOOP
    rec_tab(i).col1 := rec_tab(i).col1 + 100;
  END LOOP;

  FORALL i IN rec_tab.FIRST .. rec_tab.LAST
    UPDATE tab1 SET (col1, col2) = rec_tab(i) WHERE col1 < 8;
  OPEN c1;
  FETCH c1 BULK COLLECT
    INTO rec_tab;
  CLOSE c1;
END;

 

文件中定義的格式為:

SELECT select_items BULK COLLECT
  INTO record_variable_name
  FROM rest_of_select_stmt FETCH { cursor_name | cursor_variable_name | :host_cursor_variable_name} BULK COLLECT
  INTO record_variable_name LIMIT numeric_expression;
FORALL index IN lower_bound .. upper_bound
  INSERT INTO { table_reference | THE_subquery} {column_name, column_name .. .}
  VALUES
    (record_variable_name(index))
     rest_of_insert_stmt
    FORALL index IN lower_bound .. upper_bound
    UPDATE {table_reference | THE_subquery} alias SET
    (column_name, column_name.. .) = record_variable_name
    (index) rest_of_update_stmt
  RETURNING row_expression, row_expression .. .
  BULK COLLECT INTO record_variable_name;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16179598/viewspace-582475/,如需轉載,請註明出處,否則將追究法律責任。

相關文章