零基础学Oracle资源地址: 零基础学oracle(百度云盘资源)
declare
v_age number(3):=17;
begin
if v_age < 18 then
dbms_output.put_line('未成年禁止入内!');
end if;
end;
declare
v_age number(3) := 19;
begin
if v_age < 18 then
dbms_output.put_line('未成年禁止入内!');
else
dbms_output.put_line('大人随意!');
end if;
end;
(注意elsif写法)
declare
v_age number(3) := 69;
begin
if v_age < 18 then
dbms_output.put_line('未成年禁止入内!');
elsif v_age < 60 then
dbms_output.put_line('大人随意!');
else
dbms_output.put_line('老人需陪护方可入内!');
end if;
end;
declare
v_sex varchar2(1);
begin
v_sex := '&sex'; --&定义临时输入的变量
case v_sex
when 'M' then
dbms_output.put_line(v_sex || '是男');
when 'F' then
dbms_output.put_line(v_sex || '是女');
else
dbms_output.put_line(v_sex || '估计是妖');
end case;
end;
declare
v_month number(2);
begin
v_month := &m; --&定义临时输入的变量
case
when v_month >= 3 and v_month <= 5 then
dbms_output.put_line(v_month || '月是春天');
when v_month >= 6 and v_month <= 8 then
dbms_output.put_line(v_month || '月是夏天');
when v_month >= 9 and v_month <= 11 then
dbms_output.put_line(v_month || '月是秋天');
when v_month = 12 or v_month = 1 or v_month = 2 then --注意如何判断相等
dbms_output.put_line(v_month || '月是春天');
else
dbms_output.put_line('月份无效');
end case;
end;
declare
v_sex varchar2(2);
v_sex_desc varchar2(10);
begin
v_sex := '&sex';
v_sex_desc := case v_sex
when 'M' then
'男'
when 'F' then
'女'
else
'妖'
end;
dbms_output.put_line(v_sex_desc);
end;
--注意和前面case条件判断语句的区别: 无分号结束
declare
v_month number(2);
v_month_desc varchar2(100);
begin
v_month := &m;
v_month_desc := case
when v_month >= 3 and v_month <= 5 then
v_month || '月是春天'
when v_month >= 6 and v_month <= 8 then
v_month || '月是夏天'
when v_month >= 9 and v_month <= 11 then
v_month || '月是秋天'
when v_month = 12 or v_month = 1 or v_month = 2 then
v_month || '月是春天'
else
'月份无效'
end;
dbms_output.put_line(v_month_desc);
end;
declare
v_id number(2);
v_name varchar(50);
v_sex varchar(50);
v_sex_desc varchar(50);
begin
v_id := &id;
select name,
case sex
when 'M' then
'男'
when 'F' then
'女'
else
'妖'
end
into v_name, v_sex_desc
from customer
where id = v_id;
dbms_output.put_line(v_name || ',' || v_sex_desc);
end;
至少执行一次
declare
v_sum number := 0;
v_max number;
v_i number :=0 ;
begin
v_max := &max;
loop
v_sum := v_sum + v_i;
exit when v_i = v_max;
v_i := v_i + 1;
end loop;
dbms_output.put_line('0到' || v_max || '的总和为' || v_sum);
end;
满足条件才执行循环体
declare
v_sum number := 0;
v_max number;
v_i number :=0 ;
begin
v_max := &max;
while v_i <= v_max loop
v_sum := v_sum + v_i;
v_i := v_i + 1;
end loop;
dbms_output.put_line('0到' || v_max || '的总和为' || v_sum);
end;
declare
v_sum number := 0;
v_max number;
begin
v_max := &max;
for i in 1 .. v_max loop
v_sum := v_sum + i;
end loop;
dbms_output.put_line('总和为 ' || v_sum);
end;
declare
v_sum number := 0;
v_max number;
begin
v_max := &max;
for i in reverse 1 .. v_max loop --循环变量自动减一
v_sum := v_sum + i;
dbms_output.put_line(i);
end loop;
dbms_output.put_line('总和为 ' || v_sum);
end;
begin
for i in 1..9 loop
for j in 1..i loop
dbms_output.put(j || '*' || i || '=' || i * j || ' ');
end loop;
dbms_output.put_line(' ');
end loop;
end;
标号:标记循环,通过设定标号,可让循环退出任意标号【慎用】
declare
v_result number;
begin
<<outter>>
for i in 1 .. 5 loop
<<inner>>
for j in 1 .. 5 loop
v_result := i * j;
dbms_output.put_line(v_result);
exit outter when j = 3 and i = 4;
end loop inner;
end loop outter;
dbms_output.put_line('exit outter:' || v_result);
end;
declare
v_name customer.name%type;
v_id customer.id%type;
begin
v_id := &input;
select name into v_name from customer where id = v_id;
dbms_output.put_line(v_name);
exception
when NO_DATA_FOUND then
dbms_output.put_line('未找到id为' || v_id || '的客户!');
when others then
dbms_output.put_line('错误号:' || sqlcode || ' 描述' || sqlerrm);
end;
declare
e_user_not_exist exception;
v_count number; v_id number;
begin
v_id := &input;
select count(1) into v_count from customer where id = v_id;
if v_count = 0 then
raise e_user_not_exist;
else
dbms_output.put_line('存在id为' || v_id || '的客户!');
end if;
exception
when e_user_not_exist then
dbms_output.put_line('未找到id为' || v_id || '的客户!');
when others then
dbms_output.put_line('错误号:' || sqlcode || ' 描述' || sqlerrm);
end;
declare
v_num number;
begin
v_num := &input;
if v_num > 1000 then
dbms_output.put_line('不错');
else
null;
end if;
end;
注:
(本博客资源源于网络资料整理与个人见解结合,如有雷同相似之处,请私信作者)