Oracle (1) 基本语句操作 之 if-else\for\while循环

本文介绍了Oracle数据库中的基本语句操作,包括如何将变量与SQL语句关联,展示了IF-ELSE结构、FOR和WHILE循环的用法。通过示例解释了在Oracle SQL Developer中进行条件判断和循环控制的常见场景,如查询表中的数据、计算总和等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

示例前提

最基础的固定结构

怎么把变量和sql语句关联起来?

常见的变量声明格式

If-else结构

for、while循环的使用


 


示例前提

模仿一个通用的学校的师生信息数据库,使用sql语句已经建好了如下表,表结构如下,数据自己随意啦:

  • student 学生信息表:
sno(学号)sname(姓名)ssex(性别)sage(年龄)deptno(所在院系序号)
  • teacher 教师信息表:
tno(编号)tname(姓名)tsex(性别)grad(毕业院校)title(职位)deptno(所在院系序号)
  • score 学生成绩表:
sno(学号)cno(课程号)grade(成绩)
  • course 选课信息表:
cno(课程号)cname(课程名)ccredit(学分)institute(所属院系序号)
  • sdept 学院信息表:
deptno(院系序号)dname(院系名称)address(院系地址)telephone(负责人电话)

【注释】jdk版本和"oracle sql developer"不符合,当向表中插入数据的时候,可能导致实际值与最大值不匹配,“值过多”等报错,利用语句:alter table student  modify sname char(10) 可以为student(报错表)修改姓名字段长度。


最基础的固定结构

【通用结构】

declare

声明

begin

具体执行方法

end;

【例题】最简单的输出 

--是oracle的注释符号
declare 
  x int:=0;   --定义变量格式:变量名 数据类型:=初值; 
begin
   x:=6;        --改变x的值
  dbms_output.put_line(x);  --输出x
end;    

【输出】6

【注释】如果输出端没有任何显示,请执行代码“set serveroutput on”打开输出。

 

怎么把变量和sql语句关联起来?

【导言】在sql语句中加入into 变量名即可

【例题】查询student学生表中有多少行【输出:16】

declare 
  x int:=0;   
begin
  select count(*) into x from student;  --在sql语句中加入into 变量名即可
  dbms_output.put_line(x);  --输出总记录行数x
end;    

 

常见的变量声明格式

【普通】:就是上面例子的 x int:=0; 这种格式

【查询某一行的某一列】:假如要查询成绩表score中的分数grade这一列,x为变量名

  •  变量声明格式为:x score.grade%type;

【查询某一行】:假如要查询成绩表score中的每一行,x为变量名

  • 变量声明格式为:x score%rowtype;

【例题】查询学生高天的学号,查询于多的学号、姓名、年龄

declare
  m student.sage%type;    --可以m int:=0;这样普通定义
  x student%rowtype;      
begin
    select sage into m from student where sname='高天';
    select * into x from student where sname='于多';
    dbms_output.put_line(m);
    dbms_output.put_line(x.sno||x.sname||x.sage);
end;

【输出】

20

0802010206     于多    19

 

 


If-else结构

【通用结构】

if 条件1 then 

    执行语句

elsif 条件2 then 

    执行语句

else 

    执行语句

end if;

【例题】查询学生高天的c语言成绩在哪个等级段

declare
  x score.grade%type;   
begin
   select grade into x from score where sno=(select sno from student where sname='高天')
                                          and cno=(select cno from course where cname='C语言');
  if x>80 and x<100  then
      dbms_output.put_line(x||'优秀');
  elsif x>60 and x<80 then
      dbms_output.put_line(x||'及格');
  else
      dbms_output.put_line(x||'不及格');
  end if;
end;

【输出】45不及格

【注释】在oracle中,<>代表“不等于”。

 


for、while循环的使用

【通用结构1:结合if-else语句】(循环段和if语句顺序不同结果不同)

loop

    if-else语句(略);

    需要循环的程序段

end loop;

【通用结构2:结合while语句】

while 条件

    loop

        需要循环的程序段

    end loop;

【通用结构3:结合for语句】

for 条件

    loop

        需要循环的程序段

    end loop;

【例题1.1】输出从1+2+....+5的结果

declare
  x int:=1 ;
  total int:=0;
begin 
  loop
    if x>5 then 
      exit;     --退出,相当于break
    end if;
    total:=total+x;
    x:=x+1;
  end loop;
  dbms_output.put_line(total);
end;

【输出】15  

【解析】注意if的位置,上述代码相当于c语言中的while和for循环,先判断再执行。

 

【例题1.2】1.1的代码可以替换成:

declare
  x int:=1 ;
  total int:=0;
begin 
  loop
    total:=total+x; 
    x:=x+1;
    if x>5 then 
      exit;     
    end if;
  end loop;
  dbms_output.put_line(total);
end;

【输出】15     (15:1+2+3+4+5)

【解析】这种 if 放在循环代码段后面的,类似于c语言中的do-while,先执行再判断。然而还需要注意一个细节:

 

【例题1.3】上述代码的细节问题 

declare
  x int:=1 ;
  total int:=0;
begin 
  loop
    x:=x+1;
    total:=total+x; 
    if x>5 then 
      exit;     
    end if;
  end loop;
  dbms_output.put_line(total);
end;

【输出】     20(20:2+3+4+5+6)

【解析】注意到细节了么?total:=total+x; 和 x:=x+1;互换了位置,循环次数虽然一样,但是结果不同。

 

【例题1.4】结合for\while实现1+2+...+5

declare
  x int:=1; 
  total int:=0;
begin
  while x<=5  --该行可以替换成 for i in 1..5
  loop
     total:=total+x;
     x:=x+1;
  end loop;
  dbms_output.put_line(total);
end;

【输出】15

 

【例题2.1】遍历score表中的所有数据,并且显示每一行数据

declare
  v_score score%rowtype;  --表示表格中的某一行
  v_count int;  --当前在多少行
  v_n int default 1;  --这个表的总行数
begin
  select count(*) into v_count from score;
  loop
    select sno,cno,grade into v_score from(	 
      select rownum as num,score.* from score) 
           where num=v_n;
    dbms_output.put_line(v_score.sno||v_score.cno||v_score.grade);
    v_n:=v_n+1;
    if v_n>v_count then  
      exit;
    end if;
  end loop;
end;

【注释】虽然可以直接使用sql语句实现,但是为了说明变量和sql如何关联,以及oracle的语法,就写复杂了点

【例题2.2】使用for循环实现遍历score表

declare
  v_score score%rowtype;
  v_count int;   
begin
  select count(*) into v_count from score; 
  for i in 1..v_count
  loop
    select sno,cno,grade into v_score from(
        select rownum as num,score.* from score)   
            where num=i;
    dbms_output.put_line(v_score.sno||v_score.cno||v_score.grade); 
  end loop;
end;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值