動態sql(使用本機動態sql)

來源:互聯網
上載者:User

1.產生一個表

 

begin
execute immediate
'create table yourtable(
yourrow number,
yourdesc varchar2(20)) tablespace tabs';
end;
/

2、insert 資料

 

declare
v_yournum number;
v_yourdesc varchar2(50);
v_insert_stmt varchar2(100);
begin
v_insert_stmt := 'insert into yourtable values(:1,:2)';

v_yournum := 1;
v_yourdesc := 'one';

execute immediate v_insert_stmt using v_yournum,v_yourdesc;

v_yournum := 2;
v_yourdesc := 'two';

execute immediate v_insert_stmt using v_yournum,v_yourdesc;
end;
/

3、查詢

 

set serveroutput on;
declare 
type your_cursor_type is ref cursor;
your_cursor your_cursor_type;

type dyn_record is record(
yourrow yourtable.yourrow%type,
yourdesc yourtable.yourdesc%type);

dyn_rec dyn_record;
dynamic_select varchar2(500);
begin
dynamic_select := 'select * from yourtable order by yourrow';

open your_cursor for dynamic_select;

loop
fetch your_cursor into dyn_rec;
exit when your_cursor%notfound;

dbms_output.put_line(dyn_rec.yourrow||'   '||dyn_rec.yourdesc);
end loop;

close your_cursor;
end;
/

4、pl/sql 塊

 

--pl/sql block
declare
block_to_exec varchar2(500) := '
begin
select * into :1,:2 from yourtable where yourrow=2;
end;';
yourrow number;
yourdesc yourtable.yourdesc%type;
begin 
execute immediate block_to_exec using out yourrow, out yourdesc;--預設為in
dbms_output.put_line(yourrow||'   '||yourdesc);
end;   /

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.