程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

批處理 動態sql

編輯:更多數據庫知識

1.
DECLARE
TYPE ref_cursor_type IS ref CURSOR;
v_mycursor ref_cursor_type;
TYPE id_list IS TABLE OF integer;
TYPE name_list IS TABLE OF varchar2(30);
v_tabid id_list:=id_list();
v_tabname name_list:=name_list();
sql_str varchar2(200);
BEGIN
--查詢所以行,放在集合裡
sql_str:='select empno,ename from emp';
sql_str:=sql_str||' order by empno desc';
execute immediate sql_str BULK COLLECT INTO v_tabid,v_tabname;
FOR c IN v_tabid.first..v_tabid.last LOOP
dbms_output.put_line('empno為'||v_tabid(c)||' 記錄的NAME為'||v_tabname(c));
END LOOP;
dbms_output.put_line('---------------------------------');
--更新(返回更新後的值)
sql_str:='update emp set empno=1+empno,ename=''a'' where rownum=1 RETURNING empno,ename into :1,:2 ';
execute immediate sql_str RETURNING BULK COLLECT INTO v_tabid, v_tabname;
FOR c IN v_tabid.first..v_tabid.last LOOP
dbms_output.put_line('empno為'||v_tabid(c)||' 記錄的NAME為'||v_tabname(c));
END LOOP;
dbms_output.put_line('---------------------------------');
--刪除(返回被刪除的行)
sql_str:='delete from emp where rownum<=2 RETURNING empno,ename into :1,:2 ';
execute immediate sql_str RETURNING BULK COLLECT INTO v_tabid, v_tabname;
FOR c IN v_tabid.first..v_tabid.last LOOP
dbms_output.put_line('empno為'||v_tabid(c)||' 記錄的NAME為'||v_tabname(c));
END LOOP;
dbms_output.put_line('---------------------------------');
--插入(返回插入的行)
sql_str:='insert into emp(empno,ename) values(1,''abc'') RETURNING empno,ename into :1,:2 ';
execute immediate sql_str RETURNING BULK COLLECT INTO v_tabid, v_tabname;
FOR c IN v_tabid.first..v_tabid.last LOOP
dbms_output.put_line('empno為'||v_tabid(c)||' 記錄的NAME為'||v_tabname(c));
END LOOP;
dbms_output.put_line('---------------------------------');
/* 批fetch
語法:
fetch dynamic_cursor
bulk collect into define_variable[,define_variable...]
*/
sql_str:='select empno,ename from emp';
sql_str:=sql_str||' order by empno desc';
OPEN v_mycursor FOR sql_str;
--取
FETCH v_mycursor BULK COLLECT INTO v_tabid,v_tabname;
--關
CLOSE v_mycursor;
--輸
FOR c IN v_tabid.first..v_tabid.last LOOP
dbms_output.put_line('empno為'||v_tabid(c)||' 記錄的NAME為'||v_tabname(c));
END LOOP;
dbms_output.put_line('---------------------------------');
END;
2.-------
forall
DECLARE
/*批forall
語法:動態字符串必須為insert/update/delete,不能為select
forall index in lower..upper
execute immediate dynamic_string
using bind |bind(index)[,bind |bind(index)...]
[{returning|return} bulk collect into bind_argument[,bind_argument...]];
*/
TYPE sal_list IS TABLE OF number(8,2);
TYPE name_list IS TABLE OF varchar2(30);
TYPE dept_list IS VARRAY(15) OF integer;
v_depts dept_list:=dept_list(10,20,30,40,50,60,70,80);
v_tabsal sal_list:=sal_list();
v_tabname name_list:=name_list();
sql_str varchar2(200);
BEGIN
sql_str:='update emp set sal=sal*:arg1 where DEPTNO=:arg2';
sql_str:=sql_str||' returning ename,sal into :arg3,:arg4';
--給前面4個部門加薪10%,並返回結果到集合.
FORALL j IN 1..4
execute immediate sql_str
using 1.10,v_depts(j)
RETURNING BULK COLLECT INTO v_tabname,v_tabsal;
--顯示結果
FOR j IN v_tabname.first..v_tabname.last LOOP
dbms_output.put_line('雇員'||v_tabname(j)
||' 的薪水被提到'||v_tabsal(j));
END LOOP;
dbms_output.put_line('---------------------------------');
--給後面4個部門加薪20%,並返回結果到集合.
FORALL j IN 5..8
execute immediate sql_str
using 1.20,v_depts(j)
RETURNING BULK COLLECT INTO v_tabname,v_tabsal;
--顯示結果(用notfound判斷是否有結果集)
IF SQL%NOTFOUND THEN
dbms_output.put_line('無數據更新');
ELSE
FOR j IN v_tabname.first..v_tabname.last LOOP
dbms_output.put_line('雇員'||v_tabname(j)
||' 的薪水被提到'||v_tabsal(j));
END LOOP;
END IF;
END;
3.用一個值綁定綁定名稱相同的值.
把sql語句用begin end括起來就能實現
如:
execute immediate 'begin calc_stats(:x,:x,:y,:x,:y); end;' using a,b;
將A與X綁定,當第二次出來不同名稱時,與B綁定,以此類推

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved