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绑定,以此类推
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绑定,以此类推
标签:
批处理,sql
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“批处理 动态sql”评论...
更新动态
2024年12月27日
2024年12月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]