• PL/SQL 编程(二)


    版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u011685627/article/details/26299399


    1    For循环
        语法:begin
                for i in reverse 1..10 loop
                insert into users values(i,’奥巴马‘)。
                end loop;
              end;
        注意:循环变量 i 是隐含添加的。所以无法看到
        
    2    goto语句
        goto 语句用于跳转到特定的位置去运行语句。因为goto语句会降低程序的可读性,所以普通情况下
        不建议使用goto语句
        
    3    null语句
        null语句不会运行不论什么操作,可是能够添加程序的可读性
        
    4    创建返回值是一个结果集的存储过程
        (1) 创建一个包:
            SQL> create or replace package testpackage as
                 type test test_cursor is ref cursor;
                 end testpackage;
                
        (2) 创建存储过程
            SQL> create or replace procedure sp_procedure1
                 (stuNo in number, param_cursor out testpackage.test_cursor) is
                 begin
                    open param_cursor for select * from emp where sutno=stuNo;
                 end;
        
    5    分页
        (1) sql语句
            select * from
            (select *,rownum NO from
            (select * from emp) where rownum <=20) where rownum >=10;
            
        (2)    创建一个包
            create or replace package testpackage2 as
                 type test test_cursor is ref cursor;
                 end testpackage2;
                
        (3)    创建存储过程
            SQL> create or replace procedure procedureName2
                (tableName in varchar2,                    -- 表名
                pageSize in number,                        -- 每页显示的记录数
                pageNow in number,                        -- 当前是第几页
                pageCount out number,                    -- 总页数
                p_cursor out testpackage2.test_cursor) is
                v_sql varchar2(1000);
                v_beginNum number := (pageNow -1)* pageSize + 1;
                v_endNum number := pageNow * pageSize;
                begin
                    v_sql := 'select * from (select *,rownum NO from (select * from '|| tableName ||')
                    where rownum <= '|| v_endNum ||') where rownum >= '|| v_beginNum;
                    open p_cursor for v_sql;
                    -- 创建一个sql语句
                    v_sql := 'select count(*) from ' || tableName;
                    -- 运行sql语句,将结果保存
                    execute immediate v_sql into rows。
                    if mod(rows,pageSize) = 0
                    then pageCount := rows / pageSize;
                    else
                    pageCount := rows / pageSize + 1;
                    end if;
                    -- 关闭游标
                    close p_cursor;
                end;
        
    6    异常处理
        (1) 提前定义异常
        (2) 非提前定义异常
        (3) 自己定义异常
        
        例1
        SQL> declare v_name emp.ename%type;
             begin
             select ename into v_name from emp where empno = &no;
             dbms_output.put_line('名字:' || v_name);
             exception
                when no_data_found
                then dbms_output.put_line('编号没有!

    ');
             end;
            
        提前定义异常
        a case_not_found
          在编写case 语句时。假设在when子句中没有包括必须的条件分支(没有符合条件的)。就会触发case_not_found异常
        b cursor_already_open
          当又一次打开已经打开的游标时触发此异常
        c dup_val_on_index
          在唯一索引所相应的列上插入反复值时触发此异常
        d invalid_cursor
          当试图在不合法的游标上进行操作时触发此异常    
        e invalid_number
          当输入的数字无效时触发此异常
        f too_many_rows
          当返回值不止是一条记录时触发此异常
        g zero_divide
          当进行 x/0,即除数为零的操作时触发此异常
        h value_error
          当进行赋值操作时,假设变量的长度不足以存储实际数据时触发此异常
        i login——denide
          当用户非法登录时会触发此异常
        j not_logged_on
          假设用户没有登录就运行DML操作,就会触发此异常
        k storage_error
          假设超出了内存空间,就会触发此异常
        l timeout_on_resource
          当Oracle等待资源时。假设发生超时情况,就会触发此异常

        自己定义异常
        SQL> create or replace procedure procedureName2(sp_empNo number) is
             MyExpception Exceptiom;        -- 自己定义一个异常
             begin
             update emp set sal = sal * 1.2 where empno = &no;
             if sql%notfound then
                raise MyExpception;            -- 触发自己定义异常
             end if;
             exception
                when no_data_found
                then dbms_output.put_line('没有更新数据!

    ');
             end;

















        
        
        
        
        
       

  • 相关阅读:
    redis安装及教程
    Spring Cloud Alibaba系列教程
    EasyCode代码生成工具使用介绍
    FastDFS服务器搭建
    轻量级的java HTTP Server——NanoHttpd
    java代码的初始化过程研究
    浅谈设计模式的学习(下)
    浅谈设计模式的学习(中)
    浅谈设计模式的学习(上)
    PGET,一个简单、易用的并行获取数据框架
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10670815.html
Copyright © 2020-2023  润新知