• Oracle SQL语句收集


    //DML语句(数据操作语言)Insert、Update、  Delete、Merge 
    //DDL语句(数据定义语言)Create、Alter、  Drop、Truncate 
    //DCL语句(数据控制语言)Grant、Revoke 
    //事务控制语句Commit 、Rollback、Savepoint 
    declare   
    --定义变量    
    v_ename varchar2(5);    
     v_sal  number(7,2);    
    begin   
    --执行部分    
    select ename,sal into v_ename,v_sal from emp where empno=&aa;    
     --在控制台显示用户名    
    dbms_output.put_line('用户名是:'||v_ename||' 工资:'||v_sal);    
    --异常处理    
    exception    
    when no_data_found then   
        dbms_output.put_line('朋友,你的编号输入有误!');    
    end;   
      --输入雇员的姓名,返回该雇员的年薪    
      create function annual_incomec(name varchar2)    
      return number is   
      annual_salazy number(7,2);    
      begin   
          --执行部分    
          select sal*12+nvl(comm, 0) into annual_salazy from emp where ename=name;    
          return annual_salazy;    
      end;  
      create package sp_package is   
        procedure update_sal(name varchar2, newsal number);    
        function annual_income(name varchar2) return number;    
      end; 
    declare   
        c_tax_rate number(3,2):=0.03;    
        --用户名    
        v_ename varchar2(5);    
        v_sal number(7,2);    
        v_tax_sal number(7,2);    
      begin   
      --执行    
          select ename,sal into v_ename,v_sal from emp where empno=&no;    
    --计算所得税    
        v_tax_sal := v_sal*c_tax_rate;    
    --输出    
       dbms_output.put_line('姓名是:'||v_ename||'工资:'||v_sal||' 交税:'||v_tax_sal);    
    end;    
      declare   
        --定义一个pl/sql记录类型emp_record_type,类型包含3个数据name,salary,title。说白了,就是一个类型可以存放3个数据,主要是为了好管理    
        type emp_record_type is record(    
          name   emp.ename%type,    
          salary emp.sal%type,    
          title  emp.job%type);    
      --定义了一个sp_record变量,这个变量的类型是emp_record_type    
        sp_record emp_record_type;    
      begin   
      select ename, sal, job into sp_record from emp where empno =7788;    
      dbms_output.put_line ('员工名:' || sp_record.name);    
    end;   
      declare   
      --定义了一个pl/sql表类型sp_table_type,该类型是用于存放
    emp.ename%type    
      --index by binary_integer 表示下标是整数    
        type sp_table_type is table of emp.ename%type     
        index by binary_integer;    
      --定义了一个sp_table变量,这个变量的类型是sp_table_type    
        sp_table sp_table_type;    
      begin   
        select ename into sp_table(-1) from emp where empno = 7788;  
      
      dbms_output.put_line('员工名:' || sp_table(-1));    
    end; 
      declare   
      --定义游标sp_emp_cursor     
          type sp_emp_cursor is ref cursor;    
      --定义一个游标变量    
          test_cursor sp_emp_cursor;       
      --定义变量    
      v_ename emp.ename%type;     
      v_sal emp.sal%type;    
      begin   
    --执行    
    --把test_cursor和一个select结合    
    open test_cursor for select ename,sal from emp where deptno=&no;    
    --循环取出    
    loop    
        fetch test_cursor into v_ename,v_sal;    
        --判断是否test_cursor为空    
        exit when test_cursor%notfound;    
        dbms_output.put_line('名字:'||v_ename||' 工资:'||v_sal);    
    end loop;    
    end;    
    / 
    --SQL%NOTFOUND 是一个布尔值。与最近的sql语句(update,insert,delete,select)发生交互,当最近的一条sql语句没有涉及任何行的时候,则返回true。否则返回false。这样的语句在实际应用中,是非常有用的。例如要update一行数据时,如果没有找到,就可以作相应操作。如:
    
    begin
      update salary set bonus = 1000 where emp_id = 10;
      if sql%notfound then
         raise e_noteffact;
      end if;
    --雇员表
    create table scott_emp
    (
           empno number(4),--表示雇员编号,是唯一编号
           ename varchar2(10), --表示雇员姓名
           job varchar2(9),--表示工作职位
           mgr number(8), --表示一个雇员的领导编号
           hiredate date,--表示雇佣日期
           sal number(7,2),--表示月薪,工资
           comm number(7,2), --表示奖金,或者称为佣金
           deptno number(2) --部门编号
    )
    
    --部门表
    create table scott_dept
    (
           deptno number(2), --部门编号,是唯一编号
           dname varchar2(14),--部门名称
           loc varchar2(13) --部门设置
    )
    
    
    --工资等级表
    create table scott_salgrade
    (
           grade number, --等级名称
           losal number,--此等级的最低工资
           hisal number --此等级的最高工资
    )
    
    
    --奖金表
    create table scott_bonus
    (
          ename varchar2(10),--雇员姓名
          job varchar2(9),--雇员工作
          sal number,--雇员工资
          comm number  --雇员奖金(佣金)
    )
    -- 对于特殊符号可使用ESCAPE 标识符来查找,% 表示零或多个字符 ,_   表示一个字符 
     
    select * from emp where ename like '%*_%' escape '*' 
  • 相关阅读:
    git 学习
    C语言 do while 语句
    C语言 计算班级平均成绩以及不及格的人数
    C语言 加减算法
    C语言 两个日期间的天数计算
    C语言 梯形面积
    C语言 while语句
    C语言 分段函数if else语句
    C语言 乘法运算
    C语言学习,for循环
  • 原文地址:https://www.cnblogs.com/FH-cnblogs/p/5316745.html
Copyright © 2020-2023  润新知