oracle创建存储过程是我们经常需要用到的功能,下面就为您介绍oracle创建存储过程和其他数据库的区别,如果您感兴趣的话,不妨一看。
在oracle创建存储过程和sybase及sql server下的语法有些不一致之处。
下面就此用不同的数据库下存储过程的例子来演示之。
---------------------------
oracle创建存储过程:
- CREATE OR REPLACE FUNCTION MY_FUNC
- (
- P1 IN MY_TABLE.YY%TYPE,
- P2 IN MY_TABLE.NN%TYPE,
- P3 VARCHAR(100)
- )
- RETURN VARCHAR2 AS
/*定义有参数的游标和无参数的游标*/
- CURSOR MY_CURSOR1 IS
- SELECT YY,NN,DECODE(FYYSDM,0,'合计',1,'加工费','其他费用要素')
- FROM MY_TABLE
- WHERE YY=P1
- GROUP BY YY,NN
- ORDER BY YY,NN;
/*定义游标变量,存储游标数据集中的记录*/
- V_CURSOR1 MY_CURSOR1%ROWTYPE;
- CURSOR MY_CURSOR2(V_ZYDM MY_TABLE.ZYDM%TYPE,V_FYYSDM NUMBER) AS
- SELECT YY,NN,ZYDM,NVL(ZYCB,0) /*NVL函数转换空值为指定值*/
- FROM MY_TABLE
- WHERE YY=P1 AND NN=P2 AND ZYDM=V_ZYDM AND FYYSDM=V_FYYSDM
- GROUP BY YY,NN;
/*定义游标变量,存储游标数据集中的记录*/
- V_CURSOR2 MY_CURSOR2%ROWTYPE;
- V_CPDM MY_TABLE.CPDM%TYPE;
- V_COUNT NUMBER;
- V_BZ VARCHAR2(2);
- V_CPCB NUMBER(22,2);
- BEGIN
- V_BZ:=1;
- SELECT CPDM INTO V_CPDM FROM MY_TABLE;
- SELECT CPCB INTO V_CPCB FROM MY_TABLE WHERE ROWNUM=1;
- IF MY_CURSOR1%ISOPEN THEN /*判断游标是否已经打开*/
- CLOSE MY_CURSOR1;
- END IF;
- OPEN MY_CURSOR1;
- FETCH MY_CURSOR1 INTO V_CURSOR1;
- IF MY_CURSOR1%NOTFOUND THEN /*游标返回结果为空*/
- CLOSE MY_CURSOR1;
- RETURN(V_BZ);
- END IF;
- WHILE MY_CURSOR1%FOUND LOOP /*游标返回结果不为空*/
- V_CPDM:=V_CURSOR1.CPDM;
- V_CPCB:=V_CURSOR1.CPCB;
- V_COUNT:=100;
- IF V_COUNT=100 THEN
- V_COUNT:=99;
- END IF;
- FETCH MY_CURSOR1 INTO V_CURSOR1;
- END LOOP;
- CLOSE MY_CURSOR1;
/*显式打开带参游标*/
- SELECT CPDM INTO V_CPDM FROM MY_TABLE;
- OPEN MY_CURSOR2;
- FETCH MY_CURSOR2 INTO V_CURSOR2;
- WHILE MY_CURSOR2%FOUND LOOP /*游标返回结果不为空*/
- V_CPDM:=V_CURSOR2.CPDM;
- V_CPCB:=V_CURSOR2.CPCB;
- V_COUNT:=100;
- IF V_COUNT=100 THEN
- V_COUNT:=99;
- ELSE
- V_COUNT:=88;
- END IF;
- FETCH MY_CURSOR2 INTO V_CURSOR2;
- END LOOP;
- CLOSE MYCURSOR2;
/*隐式打开游标*/
- FOR V_CURSOR2 IN MY_CURSOR2(V_CPDM,V_CURSOR1.FYYSDM) LOOP
- IF V_CURSOR2.CPCB IS NULL THEN
- PRINT '非法!';
- ROLLBACK;
- END IF;
- UPDATE MY_TABLE
- SET CPCB=V_CPCB
- WHERE YY=P1 AND NN=P2 AND CPDM=V_CURSOR2.CPDM;
- IF SQL%NOTFOUND THEN /*判断前句是否有执行结果*/
- /*程序段*/
- END IF;
- END LOOP;
- V_BZ:=MY_DELETE_CB(P_YY,P_NN);
- IF V_BZ<>0 THEN
- PRINT '失败!';
- END IF;
- FOR I INT 1..V_COUNT LOOP
- /**/
- END LOOP;
- COMMIT;/*提交事务*/
- RETURN(0);/*要有返回值*/
- END MY_FUNC;
- CREATE OR REPLACE PROCEDURE SP_MY
- (
- P_YY IN MY_TABLE.YY%TYPE;
- P_NN NUMBER;
- )
- IS
- CURSOR MY_CURSOR IS
- SELECT CPCB
- FROM MY_TABLE
- WHERE YY=P_YY AND NN=P_NN;
- V_ZYCB NUMBER(22,2);
- BEGIN
- /**/
- /*无返回值*/
- END;
【编辑推荐】