動的SQLを実行する(Oracle) †【動的SQLで単一行を取得する1】 declare
test_rec test%rowtype;
sql1 varchar2(2000);
begin
sql1 := 'select * from test where data1 = :data1';
execute immediate sql1 into test_rec using 100;
dbms_output.put_line ('-- 取得データ --');
dbms_output.put_line (test_rec.data1);
dbms_output.put_line (test_rec.data2);
end;
/
【動的SQLで複数行を取得する(その1)】 declare
type cutype is ref cursor;
cv cutype;
item1 テストm.キー%type;
begin
open cv for
'select キー from テストm where キー like :data1'
using 'a%';
loop
fetch cv into item1;
exit when cv%notfound;
dbms_output.put_line (item1);
end loop;
close cv;
end;
/
【動的SQLで複数行を取得する(その2)】 declare
type cutype is ref cursor;
cv cutype;
rec テストm%rowtype;
begin
open cv for
'select * from テストm where キー like :data1'
using 'a%';
loop
fetch cv into rec;
exit when cv%notfound;
dbms_output.put_line (rec.キー);
end loop;
close cv;
end;
/
【動的SQLの更新結果を取得する】 declare
sql1 varchar2(2000);
ret1 number;
begin
sql1 := 'update test set data2 = :data2 where data1 = :data1
returning data2 into :ret1';
execute immediate sql1 using 11, 100 returning into ret1;
dbms_output.put_line ('-- 取得データ --');
dbms_output.put_line (ret1);
end;
/
|