set serveroutput on
begin
–打印输出
dbms_output.put_line(‘hello everyone!’);
end;
declare
v_name varchar2(10);
v_sal number(7,2);
v_hiredate date;
c_tax_rate constant number(3,2) := 0.02;
v_tax_sal number(7,2);
v_valid boolean default true;
begin
select ename,sal,hiredate
into v_name,v_sal,v_hiredate
from emp
where empno = 7369;
–计算所得税
v_tax_sal:= v_sal * c_tax_rate;
–打印输出
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 雇员日期是:’ || v_hiredate|| ‘ 所得税是:’ || v_tax_sal);
if v_valid then
dbms_output.put_line(‘已核实’);
end if;
end;
–引用型变量
declare
v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal
into v_name,v_sal
from emp
where empno = 7788;
–打印输出
dbms_output.put_line(v_name||’的工资是:’||v_sal);
end;
–记录型变量
declare
emp_record emp%rowtype;
begin
select * into emp_record from emp where empno = 7788;
–打印输出
dbms_output.put_line(emp_record.ename||’的工资是:’||emp_record.sal);
end;
–算术运算符
declare
v_num1 number(3):=10;
v_num2 number(3):=2;
begin
dbms_output.put_line(v_num1+v_num2);
dbms_output.put_line(v_num1-v_num2);
dbms_output.put_line(v_num1*v_num2);
dbms_output.put_line(v_num1/v_num2);
dbms_output.put_line(v_num1**v_num2);
end;
–关系运算符
declare
v_num1 number(2) := &n1;
v_num2 number(2) := &n2;
begin
if (v_num1 = v_num2) then
dbms_output.put_line(‘num1等于num2’);
elsif(v_num1 < v_num2) then
dbms_output.put_line(‘num1小于num2’);
elsif(v_num1 > v_num2) then
dbms_output.put_line(‘num1大于num2’);
end if;
if (v_num1 <> v_num2) then
dbms_output.put_line(‘num1不等于num2’);
end if;
end;
–比较运算符
declare
–&n1是替代变量,在执行程序时会提示输入值
v_num1 number(2):= &n1;
begin
if(v_num1 between 5 and 10 )then
dbms_output.put_line(‘num1在5到10之间’);
else
dbms_output.put_line(‘num1不在5到10之间’);
end if;
if(v_num1 in(3,8,10) )then
dbms_output.put_line(‘num1等于3,8,10中的一个值’);
else
dbms_output.put_line(‘num1不等于3,8,10中的一个值’);
end if;
if(v_num1 is null)then
dbms_output.put_line(‘num1为空’);
else
dbms_output.put_line(‘num1不为空’);
end if;
end;
–逻辑运算符
declare
v_b1 boolean := &n1;
v_b2 boolean := &n2;
begin
if(v_b1 and v_b2)then
dbms_output.put_line(‘and–true’);
end if;
if(v_b1 or v_b2)then
dbms_output.put_line(‘or–true’);
end if;
if(not v_b1)then
dbms_output.put_line(‘v_b1取反为true’);
end if;
end;
–输入员工号,判断员工工资, 显示工资小于3000的员工姓名及工资。
–简单的if语句
declare
v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal
into v_name,v_sal
from emp
where empno = &no;
if v_sal <3000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal);
end if;
end;
select * from emp;
–输入员工号,判断员工工资,将工资小于3000的员工工资涨200,并显示涨工资的员工姓名,其他员工显示员工姓名及工资。
–二重分支语句
declare
v_name empnew.ename%type;
v_sal empnew.sal%type;
v_empno empnew.empno%type := &no;
begin
select ename,sal
into v_name,v_sal
from empnew
where empno = v_empno;
if v_sal <3000 then
update empnew set sal = sal + 200 where empno = v_empno;
commit;
dbms_output.put_line(v_name||’涨工资了’);
else
dbms_output.put_line(v_name||’的工资是:’||v_sal);
end if;
end;
select * from empnew;
–输入员工号,判断员工工资, 工资小于2000,显示低收入,工资小于6000,显示中等收入,其它显示高收入。
–多重分支语句
declare
v_name empnew.ename%type;
v_sal empnew.sal%type;
begin
select ename,sal
into v_name,v_sal
from empnew
where empno = &no;
if v_sal<2000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于低收入’);
elsif v_sal<6000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于中等收入’);
else
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于高收入’);
end if;
end;
/*
–输入员工号,判断员工工资, 显示工资小于3000的员工姓名及工资。
–简单的if语句
declare
v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal
into v_name,v_sal
from emp
where empno = &no;
if v_sal <3000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal);
end if;
end;
select * from emp;
–输入员工号,判断员工工资,将工资小于3000的员工工资涨200,并显示涨工资的员工姓名,其他员工显示员工姓名及工资。
–二重分支语句
declare
v_name empnew.ename%type;
v_sal empnew.sal%type;
v_empno empnew.empno%type := &no;
begin
select ename,sal
into v_name,v_sal
from empnew
where empno = v_empno;
if v_sal <3000 then
update empnew set sal = sal + 200 where empno = v_empno;
commit;
dbms_output.put_line(v_name||’涨工资了’);
else
dbms_output.put_line(v_name||’的工资是:’||v_sal);
end if;
end;
select * from empnew;
–输入员工号,判断员工工资, 工资小于2000,显示低收入,工资小于6000,显示中等收入,其它显示高收入。
–多重分支语句
declare
v_name empnew.ename%type;
v_sal empnew.sal%type;
begin
select ename,sal
into v_name,v_sal
from empnew
where empno = &no;
if v_sal<2000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于低收入’);
elsif v_sal<6000 then
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于中等收入’);
else
dbms_output.put_line(v_name||’的工资是:’||v_sal||’ 属于高收入’);
end if;
end;
*/
–输入成级等级,判断属于哪个层次,并打印输出
–case 等值比较
declare
v_grade char(1) := ‘&no’;
begin
case v_grade
when ‘a’ then
dbms_output.put_line(‘优秀’);
when ‘b’ then
dbms_output.put_line(‘中等’);
when ‘c’ then
dbms_output.put_line(‘一般’);
else
dbms_output.put_line(‘输入有误’);
end case;
end;
–输入员工号,获取员工工资,判断工资,如果工资小于1500,补助加100,如果工资小于2500,补助加80,如果工资小于5000,补助加50.
–case 非等值比较
declare
v_sal empnew.sal%type;
v_empno empnew.empno%type := &no;
begin
select sal
into v_sal
from empnew
where empno = &no;
case
when v_sal<1500 then
update empnew set comm = nvl(comm,0)+100 where empno = v_empno;
when v_sal<2500 then
update empnew set comm = nvl(comm,0)+80 where empno = v_empno;
when v_sal<5000 then
update empnew set comm = nvl(comm,0)+50 where empno = v_empno;
–commit;
end case;
end;
select * from empnew;
–基本循环
declare
v_cnt int :=1;
begin
loop
dbms_output.put_line(v_cnt);
exit when v_cnt = 10;
v_cnt := v_cnt+1;
end loop;
end;
–while循环
declare
v_cnt int :=1;
begin
while v_cnt<=10 loop
dbms_output.put_line(v_cnt);
v_cnt := v_cnt+1;
end loop;
end;
–for循环
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
–嵌套循环
declare
v_result int;
begin
<<outter>>
for i in 1..5 loop
<<inter>>
for j in 1..5 loop
v_result:=i;
exit outter when i=4;
end loop inter;
dbms_output.put_line(‘内’||v_result);
end loop outter;
dbms_output.put_line(‘外’||v_result);
end;
–continue
declare
v_cnt int :=0;
begin
loop
v_cnt := v_cnt+1;
continue when v_cnt = 5;
dbms_output.put_line(v_cnt);
exit when v_cnt = 10;
end loop;
end;
–goto语句
declare
v_cnt int := 1;
begin
loop
dbms_output.put_line(v_cnt);
if v_cnt=10 then
–exit;
goto end_loop;
end if;
v_cnt := v_cnt + 1;
end loop;
<<end_loop>>
dbms_output.put_line(‘循环结束’);
end;
–null语句
declare
v_sal empnew.sal%type;
v_name empnew.ename%type;
begin
select ename,sal
into v_name,v_sal
from empnew
where empno = &no;
if v_sal<3000 then
update empnew set comm = nvl(comm,0)+sal*0.2 where ename=v_name;
commit;
dbms_output.put_line(v_name||’的奖金更新了’);
else
null;
end if;
end;
select * from empnew;