動態SQL運用實例 語法8.1.6之前: EXECUTE IMMEDIATE dynamic_sql_string [INTO {define_var1 [, define_var2] ... | plsql_record}] [USING [IN | OUT | IN OUT] bind_arg1 ...
動態SQL運用實例
語法
8.1.6之前:
EXECUTE IMMEDIATE dynamic_sql_string
[INTO {define_var1 [, define_var2] ... | plsql_record}]
[USING [IN | OUT | IN OUT] bind_arg1 [,
[IN | OUT | IN OUT] bind_arg2] ...];
8.1.6 開始包括 "returning into"
EXECUTE IMMEDIATE dynamic_sql_string
[INTO {define_var1 [, define_var2] ... | plsql_record}]
[USING [IN | OUT | IN OUT] bind_arg1 [,
[IN | OUT | IN OUT] bind_arg2] ...]
[{RETURNING | RETURN} INTO bind_arg3 [, bind_arg4] ...];
1.執行 DDL:
declare
str varchar2(200);
begin
str := 'create table dy_sql (dno number,dtext varchar2(120))';
EXECUTE IMMEDIATE str;
end;
/
2.執行非查詢DML:
declare
str varchar2(200);
begin
str := 'insert into dy_sql values (1,''Hello'')';
EXECUTE IMMEDIATE str;
end;
/
3.執行非查詢DML,並使用變數 :
declare
str varchar2(200);
val varchar2(20);
begin
str := 'insert into dy_sql values (2,:b1)';
val := 'SQL';
EXECUTE IMMEDIATE str USING val;
commit;
end;
/
4.執行查詢單一變數,單一筆回傳
set serverout on
declare
str varchar2(200);
val varchar2(20);
ret varchar2(20);
begin
str := 'select dtext from dy_sql where dtext = :b1';
val := 'SQL';
EXECUTE IMMEDIATE str INTO ret USING val;
dbms_output.put_line('Value fetched from table: '||ret);
end;
/
5.使用PL/SQL record type
declare
str varchar2(200);
val varchar2(20);
ret dy_sql%rowtype;
begin
str := 'select dno,dtext from dy_sql where dno = :b1';
val := 2;
EXECUTE IMMEDIATE str INTO ret USING val;
dbms_output.put_line('Value fetched number:'||ret.dno||' ; name:'||ret.dtext);
end;
/
6.Returning、OUT變數運用
declare
str varchar2(200);
val varchar2(20);
ret varchar2(20);
begin
val := 1;
str := 'delete from dy_sql where dno = :b1 returning dtext into :b2';
EXECUTE IMMEDIATE str USING val, OUT ret;
dbms_output.put_line('Deleted '||sql%rowcount||' row(s) with value: '||ret);
end;
/
7.動態創建function,並使用它,再將function 移除
declare
str varchar2(200);
val number;
ret number;
begin
begin
str := 'create or replace function doubleit (p1 in number)'||
' return number as begin return p1*2; end;';
EXECUTE IMMEDIATE str;
end;
-- Call the stored function
str := 'begin :b1 := doubleit(:b2); end;';
val := 30;
EXECUTE IMMEDIATE str USING OUT ret, IN val;
dbms_output.put_line('Result of '||val||' doubled is '||ret);
EXECUTE IMMEDIATE 'drop function doubleit';
end;
/
8.利用動態SQL暫時使用index
declare
str varchar2(200);
val varchar2(120);
ret number;
ret2 dy_sql.dtext%type;
begin
str := 'create index i_dy_sql_1 on dy_sql(dtext)';
EXECUTE IMMEDIATE str;
str := 'select dno,dtext from dy_sql where dtext = :b1';
val := 'SQL';
EXECUTE IMMEDIATE str into ret,ret2 using val ;
dbms_output.put_line('Result of dtext='||val||':dno is '||ret||',dtext is '||ret2);
str := 'drop index i_dy_sql_1 ';
EXECUTE IMMEDIATE str;
end;
/
9.宣告 REF CURSOR
declare
type my_curs_type is REF CURSOR;
curs my_curs_type;
str varchar2(200);
ret varchar2(20);
begin
str := 'select dtext from dy_sql';
OPEN curs FOR str;
loop
FETCH curs INTO ret;
exit when curs%notfound;
dbms_output.put_line(ret);
end loop;
CLOSE curs;
end;
/
10.使用變數
declare
type my_curs_type is REF CURSOR;
curs my_curs_type;
str varchar2(200);
ret dy_sql%rowtype;
val varchar2(20);
begin
str := 'select dno,dtext from dy_sql where dno <> :b1';
val := 0;
OPEN curs FOR str USING val;
loop FETCH curs INTO ret;
exit when curs%notfound;
dbms_output.put_line('Value fetched dno:'||ret.dno||',dtext:'||ret.dtext);
end loop;
CLOSE curs;
end;
/
11.使用bulk collect得到多筆資料
declare
type MY_CUR is ref cursor;
CURS MY_CUR;
row_dy_sql dy_sql%rowtype;
type STR_TAB is TABle of dy_sql%rowtype;
REC_TAB STR_TAB;
str varchar2(120);
begin
STR:= ' select dno,dtext from dy_sql';
open CURS for STR;
fetch curs bulk collect into rec_tab limit 100;
close CURS;
for I in 1..REC_TAB.count
LOOP
DBMS_OUTPUT.PUT_LINE('rec_tab:' ||REC_TAB(I).DNO ||','||REC_TAB(I).DTEXT);
end LOOP;
end;
/