ÏÂÃæ¶ÔÕâÁ½ÖÖ¸ñʽ½øÐÐ˵Ã÷£º
µÚÒ»ÖÖ¸ñʽÖеıäÁ¿ÃûÊÇÓÃÀ´´ÓÓαêÖнÓÊÕÊý¾ÝµÄ±äÁ¿£¬ÐèÒªÊÂÏȶ¨Òå¡£±äÁ¿µÄ¸öÊýºÍÀàÐÍÓ¦ÓëSELECTÓï¾äÖеÄ×ֶαäÁ¿µÄ¸öÊýºÍÀàÐÍÒ»Ö¡£
µÚ¶þÖÖ¸ñʽһ´Î½«Ò»ÐÐÊý¾ÝÈ¡µ½¼Ç¼±äÁ¿ÖУ¬ÐèҪʹÓÃ%ROWTYPEÊÂÏȶ¨Òå¼Ç¼±äÁ¿£¬ÕâÖÖÐÎʽʹÓÃÆðÀ´±È½Ï·½±ã£¬²»±Ø·Ö±ð¶¨ÒåºÍʹÓöà¸ö±äÁ¿¡£ ¶¨Òå¼Ç¼±äÁ¿µÄ·½·¨ÈçÏ£º
±äÁ¿Ãû ±íÃû|ÓαêÃû%ROWTYPE£»
ÆäÖеıí±ØÐë´æÔÚ£¬ÓαêÃûÒ²±ØÐëÏȶ¨Òå¡£ 4£®¹Ø±ÕÓαê CLOSE ÓαêÃû;
ÏÔʽÓαê´ò¿ªºó£¬±ØÐëÏÔʽµØ¹Ø±Õ¡£ÓαêÒ»µ©¹Ø±Õ£¬ÓαêÕ¼ÓõÄ×ÊÔ´¾Í±»ÊÍ·Å£¬Óαê±ä³ÉÎÞЧ£¬±ØÐëÖØдò¿ª²ÅÄÜʹÓá£
ÒÔÏÂÊÇʹÓÃÏÔʽÓαêµÄÒ»¸ö¼òµ¥Á·Ï°¡£
¡¾ÑµÁ·1¡¿ ÓÃÓαêÌáÈ¡emp±íÖÐ7788¹ÍÔ±µÄÃû³ÆºÍÖ°Îñ¡£ Sql´úÂ룺
1. SET SERVEROUTPUT ON 2. DECLARE
3. v_ename VARCHAR2(10); 4. v_job VARCHAR2(10); 5. CURSOR emp_cursor IS
6. SELECT ename,job FROM emp WHERE empno=7788; 7. BEGIN
8. OPEN emp_cursor;
9. FETCH emp_cursor INTO v_ename,v_job;
10. DBMS_OUTPUT.PUT_LINE(v_ename||','||v_job); 11. CLOSE emp_cursor; 12. END;
Ö´Ðнá¹ûΪ: Sql´úÂ룺
1. SCOTT,ANALYST
2. PL/SQL ¹ý³ÌÒѳɹ¦Íê³É¡£
˵Ã÷£º¸Ã³ÌÐòͨ¹ý¶¨ÒåÓαêemp_cursor£¬ÌáÈ¡²¢ÏÔʾ¹ÍÔ±7788µÄÃû³ÆºÍÖ°Îñ¡£ ×÷Ϊ¶ÔÒÔÉÏÀý×ӵĸĽø£¬ÔÚÒÔÏÂѵÁ·ÖвÉÓÃÁ˼Ǽ±äÁ¿¡£
¡¾ÑµÁ·2¡¿ ÓÃÓαêÌáÈ¡emp±íÖÐ7788¹ÍÔ±µÄÐÕÃû¡¢Ö°ÎñºÍ¹¤×Ê¡£ Sql´úÂ룺
1. SET SERVEROUTPUT ON 2. DECLARE
3. CURSOR emp_cursor IS SELECT ename,job,sal FROM emp WHERE empno=7788;
4. emp_record emp_cursor%ROWTYPE; 5. BEGIN
6. OPEN emp_cursor;
7. FETCH emp_cursor INTO emp_record;
8. DBMS_OUTPUT.PUT_LINE(emp_record.ename||','|| emp_record.job||','|| emp_record.sal);
9. CLOSE emp_cursor; 10. END;
Ö´Ðнá¹ûΪ: Sql´úÂ룺
1. SCOTT,ANALYST,3000 2. PL/SQL ¹ý³ÌÒѳɹ¦Íê³É¡£
˵Ã÷£ºÊµÀýÖÐʹÓüǼ±äÁ¿À´½ÓÊÕÊý¾Ý£¬¼Ç¼±äÁ¿ÓÉÓαê±äÁ¿¶¨Ò壬ÐèÒª³öÏÖÔÚÓα궨ÒåÖ®ºó¡£
×¢Ò⣺¿Éͨ¹ýÒÔÏÂÐÎʽ»ñµÃ¼Ç¼±äÁ¿µÄÄÚÈÝ£º ¼Ç¼±äÁ¿Ãû.×Ö¶ÎÃû¡£
¡¾ÑµÁ·3¡¿ ÏÔʾ¹¤×Ê×î¸ßµÄÇ°3Ãû¹ÍÔ±µÄÃû³ÆºÍ¹¤×Ê¡£ Sql´úÂ룺
1. SET SERVEROUTPUT ON 2. DECLARE
3. V_ename VARCHAR2(10);
4. V_sal NUMBER(5);
5. CURSOR emp_cursor IS SELECT ename,sal FROM emp ORDER BY sal DES
C;
6. BEGIN
7. OPEN emp_cursor;
8. FOR I IN 1..3 LOOP
9. FETCH emp_cursor INTO v_ename,v_sal; 10. DBMS_OUTPUT.PUT_LINE(v_ename||','||v_sal); 11. END LOOP; 12. CLOSE emp_cursor; 13. END;
Ö´Ðнá¹ûΪ: Sql´úÂ룺
1. KING,5000
2. SCOTT,3000 3. FORD,3000
4. PL/SQL ¹ý³ÌÒѳɹ¦Íê³É¡£
˵Ã÷£º¸Ã³ÌÐòÔÚÓα궨ÒåÖÐʹÓÃÁËORDER BY×Ó¾ä½øÐÐÅÅÐò£¬²¢Ê¹ÓÃÑ»·Óï¾äÀ´ÌáÈ¡¶àÐÐÊý¾Ý¡£ ÓαêÑ»·
¡¾ÑµÁ·1¡¿ ʹÓÃÌØÊâµÄFORÑ»·ÐÎʽÏÔʾȫ²¿¹ÍÔ±µÄ±àºÅºÍÃû³Æ¡£ Sql´úÂë
1. SET SERVEROUTPUT ON 2. DECLARE
3. CURSOR emp_cursor IS
4. SELECT empno, ename FROM emp; 5. BEGIN
6. FOR Emp_record IN emp_cursor LOOP
7. DBMS_OUTPUT.PUT_LINE(Emp_record.empno|| Emp_record.ename); 8. END LOOP; 9. END;
Ö´Ðнá¹ûΪ£º Sql´úÂë
1. 7369SMITH 2. 7499ALLEN 3. 7521WARD
4. 7566JONES
5. PL/SQL ¹ý³ÌÒѳɹ¦Íê³É¡£
˵Ã÷£º¿ÉÒÔ¿´µ½¸ÃÑ»·ÐÎʽ·Ç³£¼òµ¥£¬Òþº¬Á˼Ǽ±äÁ¿µÄ¶¨Òå¡¢ÓαêµÄ´ò¿ª¡¢ÌáÈ¡ºÍ¹Ø±Õ¹ý³Ì¡£Emp_recordΪÒþº¬¶¨ÒåµÄ¼Ç¼±äÁ¿£¬Ñ»·µÄÖ´ÐдÎÊýÓëÓαêÈ¡µÃµÄÊý¾ÝµÄÐÐÊýÏàÒ»Ö¡£
¡¾ÑµÁ·2¡¿ ÁíÒ»ÖÖÐÎʽµÄÓαêÑ»·¡£ Sql´úÂë
1. SET SERVEROUTPUT ON 2. BEGIN
3. FOR re IN (SELECT ename FROM EMP) LOOP 4. DBMS_OUTPUT.PUT_LINE(re.ename) 5. END LOOP; 6. END;
Ö´Ðнá¹ûΪ£º Sql´úÂë
1. SMITH 2. ALLEN 3. WARD 4. JONES
˵Ã÷£º¸ÃÖÖÐÎʽ¸üΪ¼òµ¥£¬Ê¡ÂÔÁËÓαêµÄ¶¨Ò壬ÓαêµÄSELECT²éѯÓï¾äÔÚÑ»·ÖÐÖ±½Ó³öÏÖ¡£
ÏÔʽÓαêÊôÐÔ
ËäÈ»¿ÉÒÔʹÓÃÇ°ÃæµÄÐÎʽ»ñµÃÓαêÊý¾Ý£¬µ«ÊÇÔÚÓα궨ÒåÒÔºóʹÓÃËüµÄһЩÊôÐÔÀ´½øÐнṹ¿ØÖÆÊÇÒ»ÖÖ¸üΪÁé»îµÄ·½·¨¡£ÏÔʽÓαêµÄÊôÐÔÈçÏÂËùʾ¡£ Sql´úÂ룺
ÓαêµÄÊôÐÔ ·µ»ØÖµÀàÐÍ ÒâÒå
1. %ROWCOUNT ÕûÐÍ »ñµÃFETCHÓï¾ä·µ»ØµÄÊý¾ÝÐÐÊý
2. %FOUND ²¼¶ûÐÍ ×î½üµÄFETCHÓï¾ä·µ»ØÒ»ÐÐÊý¾ÝÔòΪÕ棬·ñÔòΪ¼Ù 3. %NOTFOUND ²¼¶ûÐÍ Óë%FOUNDÊôÐÔ·µ»ØÖµÏà·´ 4. %ISOPEN ²¼¶ûÐÍ ÓαêÒѾ´ò¿ªÊ±ÖµÎªÕ棬·ñÔòΪ¼Ù
¿É°´ÕÕÒÔÏÂÐÎʽȡµÃÓαêµÄÊôÐÔ£º
ÓαêÃû%ÊôÐÔ
ÒªÅжÏÓαêemp_cursorÊÇ·ñ´¦ÓÚ´ò¿ª×´Ì¬£¬¿ÉÒÔʹÓÃÊôÐÔemp_cursor%ISOPEN¡£Èç¹ûÓαêÒѾ´ò¿ª£¬Ôò·µ»ØֵΪ¡°Õ桱£¬·ñÔòΪ¡°¼Ù¡±¡£¾ßÌå¿É²ÎÕÕÒÔϵÄѵÁ·¡£ ¡¾ÑµÁ·1¡¿ ʹÓÃÓαêµÄÊôÐÔÁ·Ï°¡£ Sql´úÂë
1. SET SERVEROUTPUT ON 2. DECLARE
3. V_ename VARCHAR2(10); 4. CURSOR emp_cursor IS 5. SELECT ename FROM emp; 6. BEGIN
7. OPEN emp_cursor;
8. IF emp_cursor%ISOPEN THEN 9. LOOP
10. FETCH emp_cursor INTO v_ename;
11. EXIT WHEN emp_cursor%NOTFOUND;
12. DBMS_OUTPUT.PUT_LINE(to_char(emp_cursor%ROWCOUNT)||'-'||v_ename);