参见:http://www.cnblogs.com/linjiqin/archive/2011/04/16/2018411.html
1、存储过程的语法结构:
CREATE OR REPLACE PROCEDURE 过程名 AS
声明语句段;
BEGIN
执行语句段;
EXCEPTION
异常处理语句段;
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
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
do something
END LOOP;
CLOSE C_USER;
创建存储过程实例
CREATE OR REPLACE PROCEDURE stu_proc as
--声明语句段
v_name varchar2(20);
BEGIN
--执行语句段
SELECT o.sname INTO v_name FROM student o WHERE o.id=1;
DBMS_OUTPUT.put_line(v_name);
EXCEPTION
--异常处理语句段
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.put_line('NO_DATA_FOUND');
END;
--调用存储过程
--CALL stu_proc();