1.基本结构
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER,
参数2 IN NUMBER
) IS
变量1 INTEGER :=0;
变量2 DATE;
BEGIN
54com.cn
END 存储过程名字
2.SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 变量1,变量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
3.IF 判断
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
4.while 循环
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
5.变量赋值
V_TEST := 123;
6.用for in 使用cursor
...
IS
CURSOR cur IS SELECT * FROM xxx;
BEGIN
FOR cur_result in cur LOOP
BEGIN
V_SUM :=cur_result.列名1+cur_result.列名2
END;
END LOOP;
END;
7.带参数的cursor
CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE TYPEID=C_ID;
OPEN C_USER(变量值);
LOOP 54ne.com
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
do something
END LOOP;
CLOSE C_USER;
/*************while循环*******************/
create or replace procedure stu_pro
as
v_stu_name stu_info.stu_name%type;
v_stu_id stu_info.stu_id%type;
v_stu_addr stu_info.stu_addr%type;
v_count integer;
cursor stu_cur is select * from stu_info;
begin
if stu_cur%isopen then
dbms_output.put_line('stu_cur已经被人打开了。。。');
close stu_cur;
end if;
open stu_cur;
--先fetch下
fetch stu_cur into v_stu_id,v_stu_name,v_stu_addr;
while stu_cur%found loop
if v_stu_addr='sss' then
dbms_output.put_line('------------');
update stu_info set stu_addr = 'zhang';
end if;
--循环中往下fetch
fetch stu_cur into v_stu_id,v_stu_name,v_stu_addr;
end loop;
commit;
close stu_cur;
end;
/******%rowcount属性可以知道行数**********/
/**********cursor fo 循环*****for in *****不能显示打开和提取和关闭数据*********************/
/*******不带参数的不能加括号********/
create or replace procedure stu_list
--定义异常
nonumber exception
as
cursor stu_cursor is select * from stu_info;
begin
for stu_record in stu_cursor loop
dbms_output.put_line(stu_record.stu_name||'住在'||stu_record.stu_addr);
if(stu_record.stu_addr=''){
--抛出异常
raise nonumber;
}
end loop;
exception
when nonumber then
dbms_output.put_line('没有数目');
--no_data_found是oracl自己内部定义的异常
when no_data_found then
dbms_output.put_line('no found record');
end;
/*********参数默认的是in,如果是in的话可以不写**********/
create or replace procedure get_name(v_id in student_info.stu_id%type, v_name out student_info.stu_name%type) as
begin
select addr into v_name from student_info where stu_id = v_id;
exception
when no_data_found then
raise_application_error(-20001, '你输入的ID不存在!');
end get_name;