存储过程 增删改查
1:创建包:把过程。方法都可以放到这个里面
IN表示向存储过程传递参数,OUT表示从存储过程返回参数,而IN OUT 表示传递参数和返回参数;
1 create or replace package TEST_CURD 2 as 3 procedure proc_insert( 4 sid number, 5 sname varchar2 6 ); 7 procedure proc_update( 8 update_id in number, 9 update_name in varchar2 10 ); 11 procedure proc_delete( 12 delete_id in number 13 ); 14 procedure proc_select( 15 select_id in t1.sid%type 16 ); 17 procedure proc_select2( 18 select_id in t1.sid%type 19 ); 20 procedure proc_select_cursor( 21 cursor_id in number 22 ); 23 procedure proc_select_cursor2( 24 cursor_id in number 25 ); 26 procedure proc_select_cursor3; 27 procedure proc_select_cursor4; 28 procedure proc_select_cursor5; 29 procedure proc_select_cursor6; 30 END;
2:创建包体
create or replace package body TEST_CURD as /*插入数据*/ procedure proc_insert( sid number, sname varchar2 ) is begin insert into t1 values(sid,sname); dbms_output.put_line('影响的行数:'||sql%rowcount); commit; end ; /*修改*/ procedure proc_update( update_id in number, update_name in varchar2 ) as begin update t1 set sname=update_name where sid=update_id; If SQL%Found Then DBMS_OUTPUT.put_line('更新成功!'); Else DBMS_OUTPUT.put_line('更新失败!'); end if; commit; end; /*删除*/ procedure proc_delete( delete_id in number ) as begin delete from t1 where t1.sid=delete_id; commit; end; /*查询*/ procedure proc_select( select_id in t1.sid%type --相当于update_id in number,t1.sid%type就是和t1表中的sid一个类型 ) as bianliang_id t1.sid%type; bianliang_name t1.sname%type; begin select t1.sid,t1.sname into bianliang_id,bianliang_name from t1 where t1.sid=select_id; dbms_output.put_line(bianliang_id ||'----- >' ||bianliang_name); commit; end; /*存储过程查询的值放入表中*/ procedure proc_select2( select_id in t1.sid%type ) as TEST_INSERT_BIANLIANG_ID t1.sid%type; TEST_INSERT_BIANLIANG_NAME t1.sname%type; begin select t1.sid,t1.sname into TEST_INSERT_BIANLIANG_ID,TEST_INSERT_BIANLIANG_NAME from t1 where t1.sid=select_id; insert into t2 values(TEST_INSERT_BIANLIANG_ID,TEST_INSERT_BIANLIANG_NAME); ---查询表的字段into进变量中。显示的是变量的值 dbms_output.put_line(TEST_INSERT_BIANLIANG_ID ||'----- >' ||TEST_INSERT_BIANLIANG_NAME); commit; end; /*根据游标显示单个列的字段*/ procedure proc_select_cursor( cursor_id in number ) as cursor_name t1.sname%type; ---创建 一个变量cursor_name cursor YouBiao is select sname from t1 where sid=cursor_id; ---创建一个游标。存储从t1表中根据输入的参数查询出来的字段sname。 begin open YouBiao; ---打开游标。 fetch YouBiao into cursor_name; ---把游标里的值放到变量中。 /*fetch vt. 接来(某人); 使发出; 吸引; 售得(若干价钱); vi. 取来; 抵达,到达; 卖得(好价钱); n. 拿取,拿来; 诡计; 风浪区;*/ dbms_output.put_line('游标返回单列记录' ||cursor_name); ---显示值出来 close YouBiao;---关闭游标。 end; /*根据游标显示多列*/ procedure proc_select_cursor2( cursor_id in number ) as cursor_name t1%rowtype; cursor YouBiao is select * from t1 where sid=cursor_id; begin open YouBiao; fetch YouBiao into cursor_name; dbms_output.put_line('ID:' ||cursor_name.sid||','||'NAME:'||cursor_name.sname); close YouBiao; end; /*根据游标LOOP循环查询多行记录 根据表的记录变量接收游标数据 */ procedure proc_select_cursor3 as cursor_name t1%rowtype; --表的记录变量 cursor YouBiao is select * from t1; begin open YouBiao; loop --开始循环 fetch YouBiao into cursor_name; --把存储在游标中的值放到表的记录变量中 exit when YouBiao%notfound; --exit when语句一定要紧跟在fetch之后。避免多余的数据处理。 dbms_output.put_line('ID:' ||cursor_name.sid||','||'NAME:'||cursor_name.sname); END LOOP; --结束循环 close YouBiao;--关闭游标 end; /*根据游标的记录变量接收游标数据*/ procedure proc_select_cursor4 as cursor YouBiao is select * from t1; cur_record YouBiao%rowtype; --游标的记录变量 游标%rowtype 直接是游标对应相对应的类型 begin open YouBiao; loop fetch YouBiao into cur_record; exit when YouBiao%notfound; dbms_output.put_line('ID:' ||cur_record.sid||','||'NAME:'||cur_record.sname); END LOOP; close YouBiao; end; /*简单发for循环。不用open fetch close*/ procedure proc_select_cursor5 as cursor YouBiao is select * from t1; begin for cur_record in YouBiao loop --cur_record就是变量 dbms_output.put_line('ID:' ||cur_record.sid||','||'NAME:'||cur_record.sname); END LOOP; end; /*更简单的for。select语句代替游标名。放在in后面 */ procedure proc_select_cursor6 as begin for cur_record in ( select * from t1 ) loop --cur_record就是变量 dbms_output.put_line('ID:' ||cur_record.sid||','||'NAME:'||cur_record.sname); END LOOP; end; END;--包结尾的end--