動的SQL
例
単純な例
execute immediate 'select * from hoge';
オブジェクト名の指定
execute immediate 'select * from ' || hoge;
バインド変数で値を指定。
execute immediate 'select * from hoge where id = :id' using id;
結果を取得(単一行)
rec hoge%rowtype; --レコード変数の定義
execute immediate 'select * from hoge where id = :id' into rec using id;
- 取得結果はレコード変数に。
- 単一行を戻さない場合はエラー
結果を取得(複数行)
type ctype is ref cursor;
c ctype; --カーソル変数の定義。syscursorでもよいが。
rec hoge%rowtype;
--rowtypeで定義できないものは個々のフィールドタイプでの変数定義が必要?
--例:c_id hoge.id%type;
open c for 'select * from hoge where id = :id' using id;
loop
fetch c into rec;
exit when c%notfound;
...
end loop;
close c;
なんかあればどうぞ