存储过程笔记
变量
普通变量(char,varchar2,date,number,boolean,long)
变量声明
变量名 变量类型(变量长度) 例如:vName varchar2(20);
变量赋值
- 直接赋值
:= 例如:vName := ‘德莱文’;
- 语句赋值,使用select …into…。语法:select 值 into 变量,例如:select ‘德莱文’,18 into vName ,vAge from dual;
引用类型变量
变量的类型和长度取决于表中字段的类型和长度。
通过 表名.列名%TYPE指定变量的类型和长度,例如:vName emp.e_name%TYPE;
记录型变量
接受表中的一行记录,相当于java中的对象
语法:变量名称 表名%ROWTYPE, 例如:vEmp emp%ROWTYPE;
流程控制
条件分支
语法:if 条件 then 语句;
elsif 语句 then 语句;
else 语句;
end if;
declare
var number;
psal test_procedure.money%type;
beginselect t.money into psal from test_procedure t where t.name = 'XX天';if psal>1000 then var:=10;elsif psal>1999 then var:=20;else var:=20;end if;dbms_output.put_line(var);end;
循环
-
loop循环
create or replace procedure pro_test_loop is i number; begin i:=0; loopi:=i+1;dbms_output.put_line(i);if i>5 thenexit;end if; end loop; end pro_test_loop; -
while循环:
create or replace procedure pro_test_loop is i number; begin i:=0; while i<5 loop i:=i+1; dbms_output.put_line(i); end loop; end pro_test_loop; -
for循环1:
create or replace procedure pro_test_for is i number; begin i:=0; for i in 1..5 loopdbms_output.put_line(i); end loop; end pro_test_for; -
for循环2:(使用游标)
create or replace procedure pro_test_cursor is userRow t_user%rowtype; cursor userRows is select * from t_user; begin for userRow in userRows loopdbms_output.put_line(userRow.Id|| ',' ||userRow. Name || ',' ||userRows%rowcount); end loop; end pro_test_cursor;
游标
4大属性:
属性 返回值类型 作用
sql%isopen 布尔型 判断游标是否 '开启'
sql%found 布尔型 判断游标是否 '获取' 到值
sql%notfound 布尔型 判断游标是否 '没有获取' 到值(常用于 "退出循环")
sql%rowcount 整型 '当前' 成功执行的数据行数(非 "总记录数")
例如:
-- **************************************************************
-- 功能:循环读取数据
-- 核心:先后顺序 = 先 fetch ... 再 exit when *%notfound
-- **************************************************************
declarecursor cur_stu_info isselect * from stu_info t where t.id = 9;v_stu_info cur_stu_info%rowtype;
beginopen cur_stu_info;loopfetch cur_stu_infointo v_stu_info; -- 可测试,这段 fetch 放在 %notfound 后面exit when cur_stu_info%notfound;dbms_output.put_line('该语句不会执行,因为没有 id = 9 的记录');end loop;close cur_stu_info;
end;
存储过程
带输入,输出参数
create or replace procedure insert_emp(emp_name in varchar2, dept_no in number, original_count out number, current_count out number) as
begindeclare max_empno number;beginif(emp_name is null or length(emp_name) = 0) thenreturn;end if;if(dept_no != 60) thenreturn;end if;select count(1) into original_count from emp_copy;select max(empno) into max_empno from emp_copy;insert into emp_copy(empno, ename, deptno) values(max_empno + 1, emp_name, dept_no);select count(1) into current_count from emp_copy;end;
end insert_emp;
java调用存储过程
public class OracleTest {/*** 测试存储过程* create or replace procedure queryEmpInfo(eno in number,pename out varchar2,psal out number,pjob out varchar2)asbeginselect ename,sal,job into pename,psal,pjob from emp where empno=eno;end;*/@Testpublic void testProcedure(){//格式 {call [(,, ...)]} String sql = "{call queryEmpInfo(?,?,?,?)}";Connection conn = null;CallableStatement call = null;try {conn = JdbcUtils.getConnection();call = conn.prepareCall(sql);//赋值call.setInt(1, 7839);//对于out参数,声明call.registerOutParameter(2, OracleTypes.VARCHAR);call.registerOutParameter(3, OracleTypes.NUMBER);call.registerOutParameter(4, OracleTypes.VARCHAR);//调用call.execute();//取值String name = call.getString(2);double sal = call.getDouble(3);String job = call.getString(4);System.err.println(name);System.err.println(sal);System.err.println(job);}catch(Exception e){e.printStackTrace();}finally{JdbcUtils.close(call);JdbcUtils.close(conn);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
