アットウィキロゴ
蜜蜂号覚書
掲示板 掲示板 ページ検索 ページ検索 メニュー メニュー

蜜蜂号覚書

動的SQL

最終更新:

匿名ユーザー

- view
メンバー限定 登録/ログイン

動的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;
  • open for と fetchを利用する。


なんかあればどうぞ

名前:
コメント:
記事メニュー
最近更新されたスレッド
ウィキ募集バナー