侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

精通Oracle10编程SQL(7)编写控制结构

数据库  /  管理员 发布于 2年前   165

/* *编写控制结构 */ --条件分支语句--简单条件判断DECLARE  v_sal NUMBER(6,2);BEGIN  select sal into v_sal from emp  where lower(ename)=lower('&name');  if v_sal<2000 then     update emp set sal=v_sal+200     where lower(ename)=lower('&name');  end if;end;select * from emp;--二重条件分支--如果雇员补助不是0,则在原来的基础上增加100元,如果补助为0或NULL时,则设置其补助为200元DECLARE  v_comm NUMBER(6,2);BEGIN  select comm into v_comm from emp where empno=&no;  if v_comm <> 0 then     update emp set comm=v_comm+100 where empno=&no;  else     update emp set comm=200 where empno=&no;  end if;end;--多重条件分支DECLARE  v_job VARCHAR2(10);  v_sal NUMBER(6,2);BEGIN  SELECT JOB,SAL INTO v_job,v_sal from emp where empno=&no;  if upper(v_job)=upper('president') then     update emp set sal=v_sal+1000 where empno=&no;  elsif upper(v_job)=upper('manager') then     update emp set sal=v_sal+500 where empno=&no;  else     update emp set sal=v_sal+200 where empno=&no;  end if;END;select * from emp;--CASE语句--在CASE语句中使用单一选择符进行等值比较DECLARE   v_deptno emp.deptno%TYPE;begin   v_deptno:=&no;   case v_deptno      when 1 then         update emp set comm=100 where deptno=v_deptno;      when 2 then         update emp set comm=80 where deptno=v_deptno;      when 3 then         update emp set comm=50 where deptno=v_deptno;      else         dbms_output.put_line('不存在该部门');   end case;end;select * from emp;--在CASE语句中使用多种条件比较DECLARE  v_sal emp.sal%TYPE;  v_ename emp.ename%TYPE;begin  select ename,sal into v_ename,v_sal from emp where empno=&no;  case     when v_sal<1000 then      update emp set comm=100 where ename=v_ename;    when v_sal<2000 then       update emp set comm=90 where ename=v_ename;    when v_sal<6000 then      update emp set comm=50 where ename=v_ename;  end case;end;select * from emp;--循环语句--基本循环create table temp(cola int);DECLARE  i INT:=1;begin  LOOP    insert into temp values(i);    exit when i=10;    i:=i+1;  end loop;end;select * from temp;--WHILE循环DECLARE  i INT:=1;begin  while i<=10 loop    insert into temp values(i);    i:=i+1;  end loop;end;--FOR循环begin  for i in reverse 1..10 LOOP    insert into temp values(i);  end loop;end;--嵌套循环和标号DECLARE  result int;BEGIN  <<outer>>  for i in 1..100 loop     <<inter>>     for j in 1..100 loop       result:=i*j;       exit outer when result=1000;       exit when result=500;     end loop inner;     dbms_output.put_line(result);  end loop outer;  dbms_output.put_line(result);end;--顺序控制语句--GOTODECLARE  i INT:=1;begin  LOOP     INSERT INTO temp values(i);     if i=10 then       goto end_loop;     end if;     i:=i+1;  end loop;  <<end_loop>>  dbms_output.put_line('循环结束');END;--NULL--NULL语句不会执行任何操作,并且会直接将控制传递到下一条语句--使用NULL语句的主要好处是可以提高PL/SQL程序的可读性--如果雇员工资低于300,则将其补助设置为工资的10%,如果雇员工资高于3000,则不会执行任何操作(NULL)DECLARE  v_sal emp.sal%TYPE;  v_ename emp.ename%TYPE;BEGIN  select ename,sal into v_ename,v_sal  from emp where empno=&no;  if v_sal<300 then     update emp set comm=sal*0.1 where ename=v_ename;  else     null;  end if;end;select * from emp;

 


  • 上一条:
    精通Oracle10编程SQL(5)SQL函数
    下一条:
    精通Oracle10编程SQL(8)使用复合数据类型
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在SQL Server数据库中查询慢问题的解决思路及方案(0个评论)
    • 在Sql Server数据库中锁表解锁解决办法(0个评论)
    • 在mysql中GET_LOCK、RELEASE_LOCK锁的使用示例(0个评论)
    • 在mysql数据库中any、some、all逻辑运算符使用浅析(0个评论)
    • mysql数据库SQL优化技巧十一点(0个评论)
    • 近期文章
    • 在Laravel应用程序如何减少代码重复编写(0个评论)
    • 在laravel项目中提高安全性方式推荐:CSP内容安全策略(0个评论)
    • 在go语言中从值中获取常量名称代码示例(0个评论)
    • 在go语言中如何通过名称获得结构字段和值代码示例(0个评论)
    • 在go语言中用JQuery + html2canvas实现拍摄浏览器的屏幕截图示例(0个评论)
    • 人生感悟分享:讲一个大学毕业生到社畜老狗的蜕变心路历程(0个评论)
    • laravel9框架报错Target class... does not exist解决方式(0个评论)
    • Laravel 9.48版本发布(0个评论)
    • Meta高级工程师现身说法:程序员干得越久,代码写得越少?(0个评论)
    • 本站zongscan祝大家除夕快乐,2023有奔头(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客