ºìÁªLinuxÃÅ»§
Linux°ïÖú

oracle-ÄÚ´æ±í

·¢²¼Ê±¼ä:2006-10-18 22:05:44À´Ô´:ºìÁª×÷Õß:Innovation
Ò»¡¢ ÄÚ´æ±í¸ÅÄî
1¡¢PL/SQL±íÀàËÆÓÚCÓïÑÔÖеÄÊý×é¡£Èç¹ûÒªÉùÃ÷Ò»¸öPL/SQL±í£¬ÒªÏȶ¨Òå¸Ã±íÀàÐÍ£¬È»ºóÔÚÉùÃ÷ÊôÓÚ¸ÃÀàÐ͵ıäÁ¿¡£
2¡¢ÀíÂÛÉÏ£¬Êý¾Ý¿âÊý¾Ý¿Õ¼äÓжà´ó£¬ÎÒÃǵÄÄÚ´æ±í¾Í¿ÉÒÔ´æ´¢¶à´óµÄÊý¾Ý£¬¾ÍÊÇ˵ËûºÍÎÒÃǵÄÎïÀí±íÊÇÏàͬµÄ£¬ÎÒÃÇ¿ÉÒÔ°ÑÎïÀí±íµÄÊý¾ÝÍêÈ«¿½±´µ½ÄÚ´æ±íÖС£
3¡¢PL/SQL±íµÄÔªËØûÓбØÒª°´ÕÕÌض¨µÄ´ÎÐòÅÅÁУ¬ÒòΪËûÃDz»ÊÇÏóÊý×éÄÇÑùÁ¬Ðø´æ´¢ÔÚÄÚ´æÖУ¬ÔªËØ¿ÉÒÔ°´ÕÕÈÎÒâ¼üÖµ½øÐвåÈë¡£
4¡¢PL/SQL±íµÄ¼üÖµ£¨KEY£©Ã»ÓбØÒªÊÇ˳ÐòµÄ¡£±íËùÕ¼ÓõÄÄÚ´æ²¢²»ÒÀÀµÓÚ¼üËùʹÓõÄÊýÖµ¡£
¶þ¡¢ ÉùÃ÷ÄÚ´æ±í
DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
BEGIN
NULL;
END;
ÎÒÃÇÒ²¿ÉÒÔ¶¨Òå±íÀàÐÍΪһ¸ö±íµÄËùÓÐ×ֶΣ¬ÀýÈ磺
DECLARE
-- Ö÷±í½á¹¹
TYPE T_USRREC IS RECORD(usrid tf_f_usrarch_main.usrid%TYPE,
mphonecode tf_f_usrarch_main.mphonecode%TYPE,
citycode tf_f_usrarch_main.citycode%TYPE,
servicecode tf_f_usrarch_main.servicecode%TYPE,
opendate tf_f_usrarch_main.opendate%TYPE,
firststoptime tf_f_usrarch_main.firststoptime%TYPE,
usrstatecodeset tf_f_usrarch_main.usrstatecodeset%TYPE,
utag3 tf_f_usrarch_main.utag3%TYPE,
callrankcode tf_f_usrarch_main.callrankcode%TYPE,
roamrankcode tf_f_usrarch_main.roamrankcode%TYPE,
advancepay tf_f_usrarch_main.advancepay%TYPE,
ureservvalue tf_f_usrarch_main.ureservvalue%TYPE,
creditfactor5 tf_f_usrarch_main.creditfactor5%TYPE);
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRREC_ARRAY IS TABLE OF T_USRREC INDEX BY BINARY_INTEGER;
G_USERID T_USRREC_ARRAY;
BEGIN
NULL;
END;
Èý¡¢ ¶Ô±íÀàÐ͵ÄÒýÓÃ
DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
V_USRID TF_F_USRARCH_MAIN.USRID%TYPE;
BEGIN
SELECT USRID INTO V_USRID FROM TF_F_USRARCH_MAIN WHERE MPHONECODE = ¡®13351785505¡¯;
G_USERID(1) := V_USRID;
G_USERID(2) := V_USRID;
G_USERID(10) := V_USRID;
G_USERID(-2) := V_USRID;
DBMS_OUTPUT.PUT_LINE(G_USERID(1));
DBMS_OUTPUT.PUT_LINE(G_USERID(2));
DBMS_OUTPUT.PUT_LINE(G_USERID(10));
DBMS_OUTPUT.PUT_LINE(G_USERID(-2));
END;
Ö´ÐÐʱÉèÖãºset serveroutput on
DECLARE
-- Ö÷±í½á¹¹
TYPE T_USRREC IS RECORD(usrid tf_f_usrarch_main.usrid%TYPE,
mphonecode tf_f_usrarch_main.mphonecode%TYPE,
citycode tf_f_usrarch_main.citycode%TYPE,
servicecode tf_f_usrarch_main.servicecode%TYPE,
opendate tf_f_usrarch_main.opendate%TYPE,
firststoptime tf_f_usrarch_main.firststoptime%TYPE,
usrstatecodeset tf_f_usrarch_main.usrstatecodeset%TYPE,
utag3 tf_f_usrarch_main.utag3%TYPE,
callrankcode tf_f_usrarch_main.callrankcode%TYPE,
roamrankcode tf_f_usrarch_main.roamrankcode%TYPE,
advancepay tf_f_usrarch_main.advancepay%TYPE,
ureservvalue tf_f_usrarch_main.ureservvalue%TYPE,
creditfactor5 tf_f_usrarch_main.creditfactor5%TYPE);
-- ¶¨ÒåÓαêÀàÐÍ
TYPE T_USRREC_CUR IS REF CURSOR;
RETURN T_USRREC;
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRREC_ARRAY IS TABLE OF T_USRREC INDEX BY BINARY_INTEGER;

-- ¶¨ÒåÓαê
CUR_TF_F_USRARCH_MAIN T_USRREC_CUR;
-- ¶¨ÒåÄÚ´æ±í
G_USER T_USRREC_ARRAY;
PersonKind T_USRREC;
v_cur BINARY_INTEGER;
BEGIN
v_cur := 0;
OPEN cur_tf_f_usrarch_main
FOR SELECT usrid,
mphonecode,
citycode,
servicecode,
opendate,
firststoptime,
SUBSTR(usrstatecodeset,-1,1),
utag3,
callrankcode,
roamrankcode,
advancepay,
ureservvalue,
creditfactor5
FROM tf_f_usrarch_main
WHERE removetag = '0'
AND substr(usrid, -2, 2) = '00'
AND rownum<100;
LOOP
v_cur := v_cur+1;
FETCH cur_tf_f_usrarch_main INTO PersonKind;
EXIT WHEN cur_tf_f_usrarch_main%NOTFOUND;

G_USER(v_cur).usrid := PersonKind.usrid;
G_USER(v_cur).mphonecode := PersonKind.mphonecode;
G_USER(v_cur).citycode := PersonKind.citycode;
G_USER(v_cur).servicecode := PersonKind.servicecode;
G_USER(v_cur).opendate := PersonKind.opendate;
G_USER(v_cur).firststoptime := PersonKind.firststoptime;
G_USER(v_cur).usrstatecodeset := PersonKind.usrstatecodeset;
G_USER(v_cur).utag3 := PersonKind.utag3;
G_USER(v_cur).callrankcode := PersonKind.callrankcode;
G_USER(v_cur).roamrankcode := PersonKind.roamrankcode;
G_USER(v_cur).advancepay := PersonKind.advancepay;
G_USER(v_cur).ureservvalue := PersonKind.ureservvalue;
G_USER(v_cur).creditfactor5 := PersonKind.creditfactor5;
END LOOP;

CLOSE cur_tf_f_usrarch_main;
DBMS_OUTPUT.PUT_LINE(G_USER(1).mphonecode||¡¯ ¡¯||G_USER(1).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(2).mphonecode||¡¯ ¡¯||G_USER(2).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(3).mphonecode||¡¯ ¡¯||G_USER(3).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(10).mphonecode||¡¯ ¡¯||G_USER(10).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(21).mphonecode||¡¯ ¡¯||G_USER(21).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(34).mphonecode||¡¯ ¡¯||G_USER(34).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(46).mphonecode||¡¯ ¡¯||G_USER(46).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(67).mphonecode||¡¯ ¡¯||G_USER(67).advancepay);
DBMS_OUTPUT.PUT_LINE(G_USER(89).mphonecode||¡¯ ¡¯||G_USER(89).advancepay);
END;
ËÄ¡¢ ±íµÄÊôÐÔ
count£º·µ»ØPL/SQL±íÖÐÐеĵ±Ç°ÊýÄ¿¡£
delete£ºÉ¾³ý±íÖеÄÐС£
exists£ºÈç¹ûÖ¸¶¨µÄ±íÏîÔÚ±íÖдæÔÚÄÇô·µ»Øture¡£
first£º·µ»Ø±íÖеÚÒ»ÐеÄË÷Òý¡£
last£º·µ»Ø±íÖÐ×îºóÒ»ÐеÄË÷Òý¡£
next£º·µ»Ø±íÖÐÖ¸¶¨ÐеÄÏÂÒ»ÐеÄË÷Òý¡£
prior£º·µ»Ø±íÖÐÖ¸¶¨ÐеÄÉÏÒ»ÐеÄË÷Òý¡£
ÀýÈ磺
DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
V_USRID TF_F_USRARCH_MAIN.USRID%TYPE;
BEGIN
SELECT USRID INTO V_USRID FROM TF_F_USRARCH_MAIN WHERE MPHONECODE = ¡®13351785505¡¯;
G_USERID(1) := V_USRID;
G_USERID(2) := V_USRID;
G_USERID(10) := V_USRID;
G_USERID(-2) := V_USRID;
DBMS_OUTPUT.PUT_LINE(G_USERID(1));
DBMS_OUTPUT.PUT_LINE(G_USERID(2));
DBMS_OUTPUT.PUT_LINE(G_USERID(10));
DBMS_OUTPUT.PUT_LINE(G_USERID(-2));
DBMS_OUTPUT.PUT_LINE(¡®row number sum: ¡¯|| G_USERID.COUNT);
END;
DELETE ÊôÐÔ»áɾ³ýPL/SQL±íÖеÄÐУ¬TABLE.DELETE»áɾ³ý¸Ã±íÖеÄËùÓÐÐУ¬TABLE.DELETE(i)£¬´Ó±íÖÐɾ³ýÓÉË÷ÒýiËù±ê¼ÇµÄÐУ¬TABLE.DELETE(i,j)£¬´Ó±íÖÐɾ³ýλÓÚË÷ÒýiºÍj Ö®¼äµÄËùÓÐÐС£
DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
V_USRID TF_F_USRARCH_MAIN.USRID%TYPE;
BEGIN
SELECT USRID INTO V_USRID FROM TF_F_USRARCH_MAIN WHERE MPHONECODE = ¡®13351785505¡¯;
G_USERID(1) := V_USRID;
G_USERID(2) := V_USRID;
G_USERID(10) := V_USRID;
G_USERID(-2) := V_USRID;
DBMS_OUTPUT.PUT_LINE(G_USERID(1));
DBMS_OUTPUT.PUT_LINE(G_USERID(2));
DBMS_OUTPUT.PUT_LINE(G_USERID(10));
DBMS_OUTPUT.PUT_LINE(G_USERID(-2));
IF G_USERID.EXISTS(1) THEN
DBMS_OUTPUT.PUT_LINE(¡®row number(1) exists¡¯);
END IF;
IF G_USERID.EXISTS(9) THEN
DBMS_OUTPUT.PUT_LINE(¡®row number(9) exists¡¯);
ELSE
DBMS_OUTPUT.PUT_LINE(¡®row number(9) not exists¡¯);
END IF;
END;

DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
V_USRID TF_F_USRARCH_MAIN.USRID%TYPE;
V_INDEX BINARY_INTEGER;
BEGIN
SELECT USRID INTO V_USRID FROM TF_F_USRARCH_MAIN WHERE MPHONECODE = ¡®13351785505¡¯;
G_USERID(1) := V_USRID;
G_USERID(2) := V_USRID;
G_USERID(10) := V_USRID;
G_USERID(-2) := V_USRID;
DBMS_OUTPUT.PUT_LINE(G_USERID(1));
DBMS_OUTPUT.PUT_LINE(G_USERID(2));
DBMS_OUTPUT.PUT_LINE(G_USERID(10));
DBMS_OUTPUT.PUT_LINE(G_USERID(-2));
V_INDEX := G_USERID.FIRST;
DBMS_OUTPUT.PUT_LINE(¡®FIRST ROW INDEX¡¯||¡¯ ¡¯||V_INDEX);
V_INDEX := G_USERID.LAST;
DBMS_OUTPUT.PUT_LINE(¡®LAST ROW INDEX¡¯||¡¯ ¡¯||V_INDEX);
END;

DECLARE
-- ¶¨Òå±íÀàÐÍ
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
V_USRID TF_F_USRARCH_MAIN.USRID%TYPE;
V_INDEX BINARY_INTEGER;
BEGIN
SELECT USRID INTO V_USRID FROM TF_F_USRARCH_MAIN WHERE MPHONECODE = ¡®13351785505¡¯;
G_USERID(1) := V_USRID;
G_USERID(2) := V_USRID;
G_USERID(10) := V_USRID;
G_USERID(-2) := V_USRID;
V_INDEX := G_USERID.FIRST;
LOOP
DBMS_OUTPUT.PUT_LINE(V_INDEX||¡¯ ¡®||G_USERID(V_INDEX));
V_INDEX := G_USERID.NEXT(V_INDEX);
EXIT WHEN V_INDEX = G_USERID.LAST;
END LOOP;
DBMS_OUTPUT.PUT_LINE(G_USERID.LAST||¡¯ ¡®|| G_USERID(G_USERID.LAST));
END;
Îå¡¢ BULK COLLECT ¹Ø¼ü×ÖµÄÒýÓÃ
BULK COLLECTÊÇÒ»¸öPL/SQLÓï¾ä£¬¶ø²»ÊÇSQLÓïÑÔµÄÒ»²¿·Ö¡£Òò´Ë£¬Èç¹ûÏëÓÃSQLÖ´ÐÐÒ»¸öBULK COLLECT²Ù×÷£¬ÎÒ±ØÐëÔÚÒ»¸öPL/SQL¿éÄÚ½øÐвÙ×÷¡£ÀýÈ磺
DECLARE
TYPE UsrId_Array IS TABLE OF NUMBER;
TYPE MphoneCode_Array IS TABLE OF NUMBER;
vusrid UsrId_Array;
vmphonecode MphoneCode_Array;

type test_type is table of tf_f_usrarch_main%rowtype;

CURSOR cur_tf_f_usrarch_main IS
SELECT usrid, mphonecode
FROM tf_f_usrarch_main
WHERE rownum<10;

CURSOR cur_tf_f_usrarch_main1 IS
SELECT *
FROM tf_f_usrarch_main
WHERE rownum<10;

temp NUMBER;

temp1 test_type := test_type();
BEGIN
OPEN cur_tf_f_usrarch_main;
FETCH cur_tf_f_usrarch_main BULK COLLECT INTO vusrid, vmphonecode;

dbms_output.put_line(to_char(vusrid.count));
FOR temp IN 1..vusrid.count
LOOP
dbms_output.put_line(vusrid(temp));
END LOOP;
END;
TYPE T_USRID_ARRAY IS TABLE OF TF_F_USRARCH_MAIN.USRID%TYPE INDEX BY BINARY_INTEGER;
G_USERID T_USRID_ARRAY;
BEGIN
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ