PLSQL 基础语法

01. PLSQL 语法(变量,if,loop,cursor,exception)

1. 语法

  • 在数据库服务器上保存的打断可执行方法,供其他开发人员调用

  • 可以有效的减少数据库端和服务端的数据交互,提高效率,降低带宽消耗

  • 语法格式:

    declare -- 定义部分,保存变量,引用型变量,记录型异常
    
    begin -- 逻辑处理部分
    
    exception -- 需要捕获异常是写上
    
    end; -- 结束
    

2. 定义变量

定义 描述
constant 常量
表名%rowtype 记录型变量
表名.列名%type 引用型变量
:= 给变量赋予默认值
into 把查询出来的数据进行赋值
  • 实例
declare
	-- 定义变量
	i number := 1;
	-- 定义常量
	pi constant number := 2;
	-- 定义记录型变量/记录一行数据
	-- 变量名 表名%rowtype;
	v_emp emp%rowtype;
	-- 定义引用型变量
	-- 变量名.列明%type
	pname emp.ename%type;
	
begin
	select * into pemp from emp;
end;

3. if判断

  • 语法:

    /*
    if判断:
    if 条件表达式 then
    
    elsif 条件表达式 then
    
    else 
    
    end if;
    */
    
    -- 实例:
    declare
    	age number := 18;
    begin
    	if age = 17 then
    	dbms_output.put_line('我是17');
            elsif age = 18 then
            dbms_output.put_line('我是18');
            	else
            	dbms_output.put_line('我是??');
    	end if;
    end;
    

4. loop循环

  • 语法:

    /*
    语法一: 相当于while循环
    while 条件表达式 loop
    
    end loop;
    */
    -- 实例
    declare
      i number := 0;
    begin
      while i < 10 loop
        i := i + 1;
        dbms_output.put_line(i);
      end loop;
    end;
    
    /*
    语法二:
    loop
    	exit when 退出循环条件
    
    end loop;
    */
    -- 实例:
    declare
      i number := 0;
    begin
      loop
        i := i + 1;
        exit when i > 10;
        dbms_output.put_line(i);
      end loop;
    end;
    
    /*
    语法三:
    1..10: 也可以是查询出来的结果集
    for 变量 in 1..10 loop;
    
    end loop
    */
    -- 实例:
    declare
      i number := 0;
    begin
      for i in 1..10 loop
        dbms_output.put_line(i);
      end loop;
    end;
    

5. cursor游标

  • 是一个私有的SQL工作区,分为隐式游标和显示游标,我们通常声明的是显示游标

  • 用来操作结果的,相当于java中的迭代器

  • 语法:

    /*
    开发步骤:
    	1. 声明游标: cursor 游标名 is 查询结果集
    	2. 打开游标: open 游标名
    	3. 从游标中取出数据: fetch 游标名 into 变量名
    			游标名%found: 找到数据
    			游标名%notfound: 没有找到数据
        4. 关闭游标
    */
    
    declare
      emp_rows emp%rowtype;
      -- 1.
      cursor rows is select * from emp;
    begin
      -- 2.
      open rows;
      	-- 2.1
        loop
          -- 3.
          fetch rows into emp_rows;
          -- 3.1
          exit when rows%notfound;
          dbms_output.put_line(emp_rows.ename);
        -- 3.2
        end loop;
      -- 4.
      close rows;
    end;
    

6. exception异常

类型 描述
no_data_found 找不到数据
too_many_rows 匹配到多个字符
zero_divide 零除
value_error 算数或转换异常
timeout_on_resource 在等待资源时发生超时
others 最大的异常/相当于java中的exception
  • 语法:

    /*
    yvfa:
    declare
    
    begin
    
    exception
    	when 异常类型 then
    	处理
    end;
    */
    -- 实例:
    declare
      num number := 1;
    begin
      num := num/0 ;
    exception
      when zero_divide then
        dbms_output.put_line('除零异常');
    end;
    
    -- 自定义异常:
    declare
        no_date exception;
    begin 
        raise no_date;
        
    exception
        when no_date then
          dbms_output.put_line('自定义异常');
    end;
    

7. 存储过程

  • 将一个个PLSQL的业务处理过程存储起来复用,这些被存储取来的PLSQL程序称之为存储过程

  • 实例:

    /*
    语法:
      create or replace procedure 存储过程名(参数名称 in 参数类型,返回值名称 in 返回值参数类型) is
      -- 定义变量
      begin
      
      end;
    */
    
    -- 实例一: 无参数无返回值
    create or replace procedure run1 is
    -- 声明变量
    begin
      dbms_output.put_line('run1');
    end;
    -- 调用
    begin
      run1;
    end;
    
    -- 实例二: 有参数无返回值
    create or replace procedure run2(i_sid in student.tid%type) is
    -- 声明变量
      v_sname student.sname%type;
      v_sid student.tid%type;
    begin
      select tid,sname into v_sid,v_sname from student where tid = i_sid;
      dbms_output.put_line(v_sid);
      dbms_output.put_line(v_sname);
    end;
    -- 调用
    begin
      run2(1);
    end;
    
    -- 实例三: 有参数有返回值
    create or replace procedure run3(i_sid in student.tid%type,i_sname out student.sname%type) is
    begin
      select sname into i_sname from student where tid = i_sid;
    end;
    -- 调用
    declare
      v_sname student.sname%type;
    begin
      run3(1,v_sname);
      dbms_output.put_line(v_sname);
    end;
    

你可能感兴趣的:(Oracle,数据库,plsql)