首页 > 数据库 >Oracle数据库PL/SQL学习笔记二——基础控制语句

Oracle数据库PL/SQL学习笔记二——基础控制语句

时间:2022-12-07 21:38:22浏览次数:38  
标签:end name put SQL Oracle output line PL cur


<span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">--简单的if判断</span>
<span style="font-size:18px;">begin
if &var > 10 and &var <= 50 then
dbms_output.put_line('输入值大于10');
elsif &var > 50 then
dbms_output.put_line('输入值大于50');
else
dbms_output.put_line('不在范围中');
end if;
end;

--case搜索语句 (默认搜索true,当when满足true时执行。当case false时,搜索false,当when为false时执行)
begin
case true
when &var < 10 and &var > 0 then
dbms_output.put_line('范围是0~10');
when &var < 50 and &var > 10 then
dbms_output.put_line('范围是10~50');
when &var < 100 and &var > 50 then
dbms_output.put_line('范围是50~100');
else
dbms_output.put_line('不在范围中');
end case;
end;


--简单case语句(等于比较)
declare var1 varchar2(30);
begin
var1 := &var;
case var1
when '男' then
dbms_output.put_line('性别为男');
when '女' then
dbms_output.put_line('性别为女');
else
dbms_output.put_line('不在范围中');
end case;
end;


--数值for循环
begin
for i in 1..10 Loop
dbms_output.put_line('The index value is ['||i||']');
end loop;
end;

--显示游标for循环(i相当于一条记录,能直接通过 i.字段名获得对应的字段值)
declare
cursor cur is select * from SCOTT.EMP t;
begin
for i in cur loop
dbms_output.put_line('The name is ['||i.ename||']');
end loop;
end;


--隐式游标for循环(i相当于一条记录,能直接通过 i.字段名获得对应的字段值)
begin
for i in (select * from SCOTT.EMP t) loop
if i.ename = 'JONES' then
exit;
end if;
dbms_output.put_line('The name is ['||i.ename||']');
end loop;
dbms_output.put_line('循环结束');
end;


--简单循环
declare
emp_entity SCOTT.EMP%rowtype; --定义一行的数据类型 emp_name SCOTT.EMP.ENAME%type;
cursor cur is select * from SCOTT.EMP t;
begin
open cur;
loop
fetch cur into emp_entity;
dbms_output.put_line('The name is ['||emp_entity.ename||']');
exit when cur%notfound; --exit when放在循环最后执行,达到类似repeat until循环的效果
end loop;
close cur;
end;

-- exit when的用法
declare
emp_name SCOTT.EMP.ENAME%type;--根据表的指定字段的数据类型定义变量
cursor cur is select t.ename from SCOTT.EMP t;
begin
open cur;
loop
fetch cur into emp_name;
exit when emp_name ='wanli'; --条件放前面,先判断后执行
dbms_output.put_line('The name is ['||emp_name||']');
end loop;
close cur;
end;

--if exit的用法 continue和continue when跳过当前索引值是11g的新功能
declare
emp_name SCOTT.EMP.ENAME%type;
cursor cur is select t.ename from SCOTT.EMP t;
begin
open cur;
loop
fetch cur into emp_name;
if emp_name = 'wanli' then
exit;
else
dbms_output.put_line('The name is ['||emp_name||']');
end if;
end loop;
close cur;
end;



--while循环
declare
emp_name SCOTT.EMP.ENAME%type;
cursor cur is select t.ename from SCOTT.EMP t;
begin
open cur;
while cur%isopen loop
fetch cur into emp_name;
if cur%notfound then
close cur; --关闭游标,不能通过while循环的条件判断,自然结束循环
end if;
dbms_output.put_line('The name is ['||emp_name||']');
end loop;
end;

</span>


标签:end,name,put,SQL,Oracle,output,line,PL,cur
From: https://blog.51cto.com/u_15905482/5920097

相关文章

  • Oracle数据库PL/SQL学习笔记——函数定义
    小技巧:如果在sqlwindow窗口下创建函数,但是创建的函数一直是无效函数(有个小红叉),那么可以再新建-》programwindow-》function 窗口中重写创建函数,这里面有相信的错误信......
  • Oracle数据库学习笔记四——存储过程的值传递和引用传递
    编程语言中的4种子例程:由两种行为定义,即形式值是否返回以及参数列表是值传递还是引用传递。如果返回输出,子例程就是函数,如果不返回,就是过程。所以4中子例程为:1.值传递函......
  • SQL注入的本质
    注入测试的本质:把用户输入的数据当做代码执行。两个关键条件:用户能够控制输入原本程序要执行的代码,拼接用户输入数据被执行手工测试但很多时候,Web服务器关闭了错误回显,这......
  • 解析mysql存储结构---innodb_ruby工具
    innodb_ruby用途:主要可查看innodb数据库数据表的各种存储,解析innodb的文件,用于学习数据库底层的一些存储。在debian系统安装innodb_ruby1、sudoaptinstallruby-dev2、su......
  • ezsql2
    最基本的联合查询payload:-1'unionselect1,group_concat(schema_name),3frominformation_schema.schematalimit0,1--+selectgroup_concat(schema_name)from......
  • sql注入葵花宝典
    sql注入葵花宝典重要的是找到注入点四种常见的数据库注入过程大同小异先探测出数据类型很重要注入类型union注入带错误条件注入时间延迟注入报错注入外带交互的......
  • mysql explain
    explain这个命令来查看SQL语句的执行计划,查看该SQL语句有没有使用上了索引,有没有做全表扫描等执行explain后,显示的信息有如下几列id:表示查询中执行select子句或操......
  • 初探spring事件applicationEvent
    前言不知道各位小伙伴对事件(event)这个比较抽象的名词如何理解,从我实际开发和使用经验来说,事件通常指的是某一特定条件下触发的一组操作。做过生态开发(ISV)的小伙伴一定对......
  • [BUUCTF][Web][SUCTF 2019]EasySQL 1
    这一题有点蛋疼,比较难顶看了别人的writeup也很难get到解题思路,感觉必须要拿到源码进行审计才能解大佬们猜后端是这么写的select$_POST['query']||flagfromFlag;......
  • mybatis-plus简介
    看到这个名字,就知道它是一个mybatis的增强版。个人觉得mybatis-plus同时拥有了JPA的便捷性和mybatis的灵活性,可谓集二者之优点:当你只需要简单的CRUD的时候,它内置了许多方......