¡¡¡¡ PL/SQL³ÌÐòÖеÄÁ÷³Ì¿ØÖÆÓï¾ä½è¼øÁËÐí¶à¸ß¼¶ÓïÑÔµÄÁ÷³Ì¿ØÖÆ˼Ï룬µ«ÓÖÓÐ×Ô¼ºµÄÌص㡣
Ìõ¼þ¿ØÖÆ
ÏÂÃæͨ¹ýʵÀý½éÉÜÌõ¼þ¿ØÖÆÓï¾äµÄʹÓá£
1. if..then..end ifÌõ¼þ¿ØÖÆ
²ÉÓÃif..then..end ifÌõ¼þ¿ØÖƵÄÓï·¨½á¹¹Èçͼ9.15Ëùʾ¡£
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐòÅжÏÁ½¸öÕûÊý±äÁ¿µÄ´óС¡£ Ö´Ðнá¹ûÈçͼ9.16Ëùʾ¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=90;
number2 integer:=60;
begin
if number1>=number2 then
dbms_output.put_line('number1´óÓÚµÈÓÚnumber2');
end if;
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\ conditioncontrol1.sql¡£
2. if..then..else..end ifÌõ¼þ¿ØÖÆ
²ÉÓÃif..then..else..end ifÌõ¼þ¿ØÖƵÄÓï·¨½á¹¹Èçͼ9.17Ëùʾ¡£
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐòÅжÏÁ½¸öÕûÊý±äÁ¿µÄ´óС£¬Êä³ö²»Í¬µÄ½á¹û¡£Ö´Ðнá¹ûÈçͼ9.18Ëùʾ¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
begin
if number1>=number2 then
dbms_output.put_line('number1´óÓÚµÈÓÚnumber2');
else
dbms_output.put_line('number1СÓÚnumber2');
end if;
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\ conditioncontrol2.sql¡£
3. ifǶÌ×Ìõ¼þ¿ØÖÆ
²ÉÓÃifǶÌ×Ìõ¼þ¿ØÖƵÄÓï·¨½á¹¹Èçͼ9.19Ëùʾ¡£
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐòÅжÏÁ½¸öÕûÊý±äÁ¿µÄ´óС£¬Êä³ö²»Í¬µÄ½á¹û¡£
Ö´Ðнá¹ûÈçͼ9.20Ëùʾ¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
begin
if number1<=number2 then
if number1=number2 then
dbms_output.put_line('number1µÈÓÚnumber2');
else
dbms_output.put_line('number1СÓÚnumber2');
end if;
else
dbms_output.put_line('number1´óÓÚnumber2');
end if;
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\ conditioncontrol3.sql¡£
Ñ»·¿ØÖÆ
Ñ»·½á¹¹ÊÇ°´ÕÕÒ»¶¨Âß¼Ìõ¼þÖ´ÐÐÒ»×éÃüÁPL/SQLÖÐÓÐ4ÖÖ»ù±¾Ñ»·½á¹¹£¬ÔÚËüÃÇ»ù´¡ÉÏÓÖ¿ÉÒÔÑݱä³öÐí¶àǶÌ×Ñ»·¿ØÖÆ£¬ÕâÀï½éÉÜ×î»ù±¾µÄÑ»·¿ØÖÆÓï¾ä¡£
1. loop..exit..end loopÑ»·¿ØÖÆ
²ÉÓÃloop..exit..end loopÑ»·¿ØÖƵÄÓï·¨½á¹¹Èçͼ9.21Ëùʾ¡£
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐò½«number1±äÁ¿Ã¿´Î¼Ó1£¬Ò»Ö±µ½µÈÓÚnumber2Ϊֹ£¬Í³¼ÆÊä³öÑ»·´ÎÊý¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=0;
begin
loop
number1:=number1+1;
if number1=number2 then
exit;
else
i:=i+1;
end if;
end loop;
dbms_output.put_line('¹²Ñ»·´ÎÊý:'||to_char(i));
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
Ö´Ðнá¹ûÈçͼ9.22Ëùʾ¡£
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\loopcontrol1.sql¡£
2. loop..exit..when..end loopÑ»·¿ØÖÆ
²ÉÓÃloop..exit..when..end loopÑ»·¿ØÖƵÄÓï·¨½á¹¹Óëͼ9.21Ëùʾ½á¹¹ÀàËÆ¡£
exit whenʵ¼ÊÉϾÍÏ൱ÓÚ
if Ìõ¼þ then
exit;
end if;
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐò½«number1±äÁ¿Ã¿´Î¼Ó1£¬Ò»Ö±µ½µÈÓÚnumber2Ϊֹ£¬Í³¼ÆÊä³öÑ»·´ÎÊý¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=0;
begin
loop
number1:=number1+1;
i:=i+1;
exit when number1=number2;
end loop;
dbms_output.put_line('¹²Ñ»·´ÎÊý:'||to_char(i));
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
Ö´Ðнá¹ûÈçͼ9.23Ëùʾ¡£
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\loopcontrol2.sql¡£
whenÑ»·¿ØÖƽáÊøÌõ¼þ±È²ÉÓÃifµÄÌõ¼þ¿ØÖƽáÊøÑ»·´ÎÊý¶à1´Î¡£
3. while..loop..end loopÑ»·¿ØÖÆ
²ÉÓÃloop..exit..when..end loopÑ»·¿ØÖƵÄÓï·¨ÈçÏ¡£
while Ìõ¼þ loop
Ö´ÐÐÓï¾ä¶Î;
end loop;
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐò½«number1±äÁ¿Ã¿´Î¼Ó1£¬Ò»Ö±µ½µÈÓÚnumber2Ϊֹ£¬Í³¼ÆÊä³öÑ»·´ÎÊý¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=0;
begin
while number1<number2 loop
number1:=number1+1;
i:=i+1;
end loop;
dbms_output.put_line('¹²Ñ»·´ÎÊý:'||to_char(i));
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
Ö´Ðнá¹ûÈçͼ9.24Ëùʾ¡£
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\whilecontrol.sql¡£
4. for..in..loop..endÑ»·¿ØÖÆ
²ÉÓÃfor..in..loop..endÑ»·¿ØÖƵÄÓï·¨ÈçÏ¡£
for Ñ»·±äÁ¿ in [reverse] Ñ»·Ï½ç..Ñ»·ÉϽç loop
Ñ»·´¦ÀíÓï¾ä¶Î;
end loop;
ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ´ÐÐÏÂÁÐPL/SQL³ÌÐò£¬¸Ã³ÌÐòͨ¹ýÑ»·±äÁ¿IÀ´¿ØÖÆnumber1Ôö¼Ó´ÎÊý£¬Êä³ö½á¹û¡£Ö´Ðнá¹ûÈçͼ9.25Ëùʾ¡£
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=0;
begin
for i in 1..10 loop
number1:=number1+1;
end loop;
dbms_output.put_line('number1µÄÖµ:'||to_char(number1));
end;
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
¡¾ÅäÌ׳ÌÐòλÖá¿£ºµÚ9ÕÂ\forcontrol.sql¡£
PL/SQLÁ÷³Ì¿ØÖÆ
80¿á¿áÍø 80kuku.com