特性:函数被调用一次之后就会被缓存在服务器! 查看有哪些函数 USER_PROCEDURES SELECT * FROM USER_PROCEDURES; SELECT OBJECT_NAME, OBJECT_ID, OBJECT_TYPE FROM USER_PROCEDURES ORDER BY OBJECT_TYPE; 查看具体函数source USER_SOURCE `` SELECT NAME, LINE, TEXT FROM USER_SOURCE WHERE NAME='FINDGIRL';
-- 开启在屏幕显示 SET SERVEROUTPUT ON; -- CREATE OR REPLACE CREATEFUNCTION FINDGIRL -- IN OUT (SEX INCHAR) RETURN VARCHAR2 IS BEGIN IF SEX='1'THEN --DBMS_OUTPUT.PUT_LINE('HH, FIND A GIRL'); RETURN ('GIRL'); ELSE --DBMS_OUTPUT.PUT_LINE('EEE, NOT SO LUCKY'); RETURN('COW SHIT'); END IF; END; /
SELECT NAME, SEX, FINDGIRL(SEX) FROM STUDENT; --删除函数 DROPFUNCTION FINDGIRL; EXIT;
DECLARE CURSOR CUR_STUDENT ISSELECT*FROM STUDENT; V_STUDENT STUDENT%ROWTYPE; BEGIN IF CUR_STUDENT%ISOPEN THEN FETCH CUR_STUDENT INTO V_STUDENT; DBMS_OUTPUT.PUT_LINE('NAME : '|| V_STUDENT.NAME); ELSE DBMS_OUTPUT.PUT_LINE('cursor is not open'); END IF; END; / EXIT;
DECLARE CURSOR CUR_STUDENT ISSELECT*FROM STUDENT; V_STUDENT STUDENT%ROWTYPE; BEGIN OPEN CUR_STUDENT; LOOP FETCH CUR_STUDENT INTO V_STUDENT; DBMS_OUTPUT.PUT_LINE(CUR_STUDENT%ROWCOUNT ||'ROWS'); IF CUR_STUDENT%FOUND THEN DBMS_OUTPUT.PUT_LINE(V_STUDENT.NAME); ELSE DBMS_OUTPUT.PUT_LINE('no data found'); EXIT; END IF; END LOOP; CLOSE CUR_STUDENT; END; / EXIT;
DECLARE CURSOR CUR_STUDENT (SID NUMBER) ISSELECT*FROM STUDENT WHERE ID=SID; V_STUDENT STUDENT%ROWTYPE; BEGIN OPEN CUR_STUDENT(1); LOOP FETCH CUR_STUDENT INTO V_STUDENT; IF CUR_STUDENT%FOUND THEN DBMS_OUTPUT.PUT_LINE(V_STUDENT.NAME); ELSE DBMS_OUTPUT.PUT_LINE('no data found'); EXIT; END IF; END LOOP; CLOSE CUR_STUDENT; END; / EXIT;
隐式游标
隐式游标由PL/SQL自动管理,默认名称为SQL。
1 2 3 4 5 6 7 8 9 10 11 12
SET SERVEROUTPUT ON;
DECLARE V_STUDENT STUDENT%ROWTYPE; BEGIN SELECT*INTO V_STUDENT FROM STUDENT WHERE ID=2; IF SQL%FOUND THEN DBMS_OUTPUT.PUT_LINE(V_STUDENT.NAME); END IF; END; / EXIT;