oracle plsql基本语法

oracle plsql 基本语法

--plsql默认规则:plsql赋值用":=" plsql判断用"=" plsql输入用"&"

--输入输出
declare
  v_str varchar2(20);
begin
 -- Dbms_Output.put('hello');
 -- Dbms_Output.put('xxxxxxxxxxxxxxxxxx');
 -- Dbms_Output.put_line('');  put_line('')才会最终输出,put只会在缓存中    
 v_str:='&str';
 Dbms_Output.put_line(v_str); 
  
end;

--plsql的判断if句式
declare
 v_age number:=0;
 begin
  v_age:='&age';
  if(v_age<18) then
   Dbms_Output.put_line('未成年');
  elsif(v_age>18 and v_age<28) then
   Dbms_Output.put_line('青年');
  else
   Dbms_Output.put_line('其他年龄');
  end if;
 end;
 
 --plsql的判断case句式
 declare
  v_empno scott.emp.empno%type;
  v_sal scott.emp.sal%type;
begin
  v_empno:='&empno';
  select sal into v_sal from scott.emp where empno=v_empno;
  case--case开始
      when v_sal<1000 then 
       Dbms_Output.put_line('底层员工');
      when v_sal>=1000 and v_sal <=2000 then
       Dbms_Output.put_line('普通员工');
      when v_sal>2000 and v_sal <=3000 then
       Dbms_Output.put_line('中级骨干');
      when v_sal>3000 then
       Dbms_Output.put_line('管理层');
      else --默认
       Dbms_Output.put_line('未知级别');
  end case;--case结束
end;


--练习 输入职员编号 判断该职业叫做king则输出老板,叫做scott输出作者,其他输出普通员工
declare
 v_empno scott.emp.empno%type;
 v_ename scott.emp.ename%type;
begin
 v_empno:='&empno';
 select ename into v_ename from scott.emp where empno=v_empno;
 case
        when v_ename='KING' then
         Dbms_Output.put_line('老板');
        when v_ename='SCOTT' then
         Dbms_Output.put_line('作者');
        else
         Dbms_Output.put_line('其他员工');
 end case;
end;

select * from emp;

 --简化case句式 直接是一条sql查询语句
select empno,
case ename
when 'KING' then '老板'
when 'SCOTT' then '作者'
else '普通职员'
end 身份
 from emp;
 
--plsql 循环while句式:从1累加到100
declare
 v_i number:=1;
 v_sum number:=0;
begin
 while(v_i<=100) loop
   v_sum:=v_sum+v_i;
   v_i:=v_i+1;
 end loop;
 Dbms_Output.put_line('累加结果为:'||v_sum);
end;
 
 
--plsql 循环do ... while句式:从1累加到100
declare
 v_i number:=1;
 v_sum number:=0;
begin
 while(1=1) loop
   v_sum:=v_sum+v_i;
   v_i:=v_i+1;
   exit when(v_i>100);      --像java中的break
 end loop;
 Dbms_Output.put_line('累加结果为:'||v_sum);
end;

--plsql 循环for句式:从1累加到100
declare
 v_i number:=1;
 v_sum number:=0;
begin
 for v_i in /*reverse*/ 1..100 loop
  Dbms_Output.put_line('v_i:'||v_i);
  v_sum:=v_sum+v_i;
 end loop;
 Dbms_Output.put_line('累加结果为:'||v_sum);
end;
 

--练习 使用while循环输出* ,打印出如下图形
declare
  v_triLength number;
  v_i number:=1;
  v_j number:=1;
begin
  v_triLength:='&边长';
  while( v_i<=v_triLength) loop
   v_j:=1;
   --打空格
   while(v_j<=v_triLength-v_i) loop
     Dbms_Output.put(' '); 
        v_j:=v_j+1;
   end loop;
   --打星星
      v_j:=1;
   while(v_j<=v_i) loop
     Dbms_Output.put('*'||' ');
        v_j:=v_j+1;
   end loop;
     Dbms_Output.put_line('');
   v_i:=v_i+1;
  end loop;
end;

转载于:https://www.cnblogs.com/Jotal/p/11381859.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值