当前位置: 数据库>sqlserver
批处理 动态sql
来源: 互联网 发布时间:2014-09-05
本文导语: 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 emp...
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
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