0

在 postresql 函数中,我们可以通过 FOR i IN (SELECT ...) 列出表

CREATE OR REPLACE FUNCTION f()
  RETURNS trigger AS
$BODY$
DECLARE
table_to_row RECORD;
BEGIN
<<for_loop>>
FOR table_to_row IN SELECT id FROM table1
LOOP
    //do something
END LOOP for_loop;
RETURN NULL;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

我们如何在 Oracle 11g 中做这样的事情?我需要这部分

FOR table_to_row IN SELECT id FROM table1
LOOP
   //do something
END LOOP;
4

1 回答 1

0

在 Oracle 中,您不必显式声明游标循环变量。它隐含地属于table_name%rowtype数据类型。

这是一个例子:

For i in (select col_1, .., col_n
            from your_table)
loop
  -- do something
end loop;

示范:

create table test_1(col1 varchar2(11));

Table created

SQL> insert into test_1(col1) values('data_1');

1 row inserted

SQL> insert into test_1(col1) values('data_2');

1 row inserted

SQL> insert into test_1(col1) values('data_3');

1 row inserted

SQL> commit;

Commit complete

SQL> set serveroutput on;

SQL>begin
  2    for i in (select col1 from test_1)
  3    loop
  4      dbms_output.put_line(i.col1);
  5    end loop;
  6  end;
  7  /

data_1
data_2
data_3

PL/SQL procedure successfully completed
于 2012-10-17T10:13:35.007 回答