2012-10-17 60 views
0

在postresql功能,我們可以通過因我列出表(SELECT ...)如何列出表中的Oracle功能

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; 

回答

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 
+0

什麼是我的類型? –

+0

我已經在 –

+0

上添加了一些細節謝謝,我認爲我們需要聲明我 –