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(); v_tabid id_list:=id_list(); v_tabname name_list:=name_list(); sql_str varchar2(); 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(); TYPE dept_list IS VARRAY() OF integer; v_depts dept_list:=dept_list(,,,,,,,); v_tabsal sal_list:=sal_list(); v_tabname name_list:=name_list(); sql_str varchar2(); BEGIN sql_str:='update emp set sal=sal*:arg1 where DEPTNO=:arg2'; sql_str:=sql_str||' returning ename,sal into :arg3,:arg4'; --给前面4个部门加薪%,并返回结果到集合. FORALL j IN 1..4 execute immediate sql_str using 1.,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个部门加薪%,并返回结果到集合. FORALL j IN 5..8 execute immediate sql_str using 1.,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(sqlserver批处理),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:批处理执行sql文件,批量执行sql脚本,sqlplus批处理命令,sqlserver批处理,批处理执行sql文件,批处理执行sql脚本,sqlplus批处理命令,批处理执行sql,内容如对您有帮助,希望把文章链接给更多的朋友!
SQL Server 的逻辑查询处理步骤 最近我则刚刚收到了微软最有价值专家项目组寄来的该书的SQLServer英文版本-《InsideMicrosoftSQLServer:T-SQLQuerying》(
在sp_executesql中使用like字句的方法 declare@LikeSqlnvarchar();--定义一个like变量,如果是存储过程,此处可以存储过程的参数set@LikeSql=‘someword%';--把LikeSql变量赋值,可以赋值成'%xxxxxxxxxxxx%'decl
SQL 合并多行记录的方法总汇 SQL中合并多行记录的方法总汇:--1.创建表,添加测试数据CREATETABLEtb(idint,[value]varchar())INSERTtbSELECT1,'aa'UNIONALLSELECT1,'bb'UNIONALLSELECT2,'aaa'UNIONALLSELECT2,'bbb'UNION