oracle控制语句学习

一、利用控制语句打印九九乘法表
set severout on//使控制台能够进行输出
declare//变量声明
:=//给变量赋值
||//连接符相当于JAVA中的连接符“+”没有实际含义
exit//退出循环

set serverout on;
declare sum1 number :=0;
temp varchar2(500);
s number :=0;
begin
for i in 1..9 loop
temp := '';
s:=(10-i);
for j in 1 .. s
loop
sum1 := (10-i) * (10-j);
temp := temp||to_char(10-j) || ' * ' ||to_char(10-i) ||' = ' ||to_char(sum1) ||' ';
end loop;
dbms_output.put_line(temp );
end loop;
end;

二、其他的一些操作

set serverout on;
declare n number:=1;
        v varchar2(20):='world';
begin
  dbms_output.put_line('hello'||n||v);
end;


set serverout on;
declare emp_count number:=1;

begin
  select count(*) into emp_count from emp where sal>=3000;
  if emp_count=1 then
     dbms_output.put_line('有1个员工的薪资大于等于3000');
  else if emp_count>1 then
    dbms_output.put_line('有超过一个员工的薪资大于等于3000');
  else
    dbms_output.put_line('没有员工的薪资大于等于3000');
  end if;
  end if;
end;

set serverout on;
declare emp_count number:=1;

begin
   select count(*) into emp_count from emp where sal>=3000;
   case emp_count
     when 0 then dbms_output.put_line('没有员工的薪资大于等于3000');
     when 1 then dbms_output.put_line('有1个员工的薪资大于等于3000');
     when 2 then dbms_output.put_line('有2个员工的薪资大于等于3000');
     when 3 then dbms_output.put_line('有3个员工的薪资大于等于3000');
     else
       dbms_output.put_line('有超过3员工的薪资大于等于3000');
    end case;
end;



set serverout on;
declare g_id number:=2;
        g_losal number;
        g_hisal number;
begin
  loop
    if(g_id>4) then
       exit;
    end if;

    select losal,hisal into g_losal,g_hisal from salgrade where grade=g_id;
    dbms_output.put_line(g_id||'最低薪资'||g_losal||'最高薪资'||g_hisal);

    g_id:=g_id+1;
  end loop;
end;

猜你喜欢

转载自blog.csdn.net/qq_31681017/article/details/75633732
今日推荐