`
wanglihu
  • 浏览: 909758 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类

PL/SQL编程(高级特性)

阅读更多
PL/SQL编程(高级特性)2009年04月09日 星期四 11:20--set serveroutput On--打开显示模式
declare
     type cust_record_type is record(--定义记录类型
         customer_name customer.customer_name%type,--声明标量变量  
         customer_status customer.customer_status%type--声明记录变量  
     );  
     cust_record cust_record_type;  
begin
     select a.customer_name ,a.customer_status into cust_record from customer a Where Rownum=1 ;
     --where a.customer_id=b.customer_id and b.ord_id=&id;  
     dbms_output.put_line('客户名:'||cust_record.customer_name);  
     dbms_output.put_line('状态:'||cust_record.customer_status);  
end;   



/*********************************************************   
                                                      
    Author:qinyangzhao                                  
    describe:%rowtype属性(rowtype)                              
                                                       
*********************************************************/   
declare   
     product_record product%rowtype;   
begin  
     product_record.product_id:=&id;   
     product_record.description:='&description';   
    insert into product values product_record;   
end;     
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:索引表(table)                               
                                                       
*********************************************************/       
declare  
     type item_table_type is table of item.ename%type   
    index by pls_integer;   
     item_table item_table_type;   
begin  
    select * bulk collect into item_table(-1)   
    from item where ord_id=&id;      
     dbms_output.put_line('条款编号:'||item_table(-1));   
       
end;           
  
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:嵌套表(table)                               
                                                       
*********************************************************/       
declare  
     type item_table_type is table of item.ename%type;      
     item_table item_table_type;   
begin  
     item_table:=item_table_type('mary','mary','mary');   
    select ename into item_table(2) from item   
    where empno=&no;   
     dbms_output.put_line('雇员名:'||item_table(2));   
end;     
/*********************************************************   
                                                      
    Author:qinyangzhao                                  
    describe:变长数组(array)                                
                                                      
*********************************************************/   
declare  
  
     type name_array_type is varray(20) of varchar2(30);   
     type city_array_type is varray(20) of varchar2(30);   
     name_array name_array_type;   
     city_array city_array_type;   
begin           
    select name ,city bulk collect   
        into name_array,city_array from customer;   
    for i in 1..name_array.count loop   
         dbms_output.put_line('客户名:'||name_array(i)||',所在城市:'||city_array(i));   
    end loop;       
end;   
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:记录表(table)                               
                                                       
*********************************************************/       
declare  
     type item_table_type is table of item%rowtype   
    index by pls_integer;   
     item_table item_table_type;   
begin  
    select * bulk collect into item_table   
    from item where ord_id=&id;   
    for i in 1..item_table.count loop   
         dbms_output.put_line('条款编号:'||item_table(i).item_id||',总价:'||   
             item_table(i).total);   
     end loop;   
end;        
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:多级(varray)                               
                                                       
*********************************************************/   
declare  
     type al_varray_type is varray(10) of int;--定义一维varray   
     type nal_varray_type is varray(10) of al_varray_type;--定义二维varrary集合   
    --初始化二维集合变量   
     nvl nal_varrary_type:=nal_varrary_type(   
         al_varray_type(58,100,102),   
         al_varray_type(55,6,73),   
         al_arrary_type(2,4));   
begin  
     dbms_output.put_line('显示二维数组所有元素');   
    for i in 1..nvl.count loop   
        for j in 1..nvl(i).count loop   
             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
        end loop;   
    end loop;   
end;      
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:多级(嵌套)                               
                                                       
*********************************************************/   
declare  
     type al_table_type is table of int;--定义一维嵌套表   
     type nal_table_type is table of al_table_type;--定义二维嵌套表集合   
    --初始化二维集合变量   
     nvl nal_varrary_type:=nal_varrary_type(   
         al_varray_type(58,100,102),   
         al_varray_type(55,6,73),   
         al_arrary_type(2,4));   
begin  
     dbms_output.put_line('显示二维数组所有元素');   
    for i in 1..nvl.count loop   
        for j in 1..nvl(i).count loop   
             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
        end loop;   
    end loop;   
end;     
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:多级(索引表)                               
                                                       
*********************************************************/   
declare  
     type al_table_type is table of int  
    index by binary_integer;--定义一维table   
     type nal_table_type is table of al_table_type   
    index by binary_integer;--定义二维table集合    
     nvl nal_varrary_type;   
begin  
    --初始化二维集合变量   
     nvl(1)(1):=10;   
     nvl(1)(2):=5;   
     nvl(2)(1):=32;   
     nvl(2)(2):=88;   
     dbms_output.put_line('显示二维数组所有元素');   
    for i in 1..nvl.count loop   
        for j in 1..nvl(i).count loop   
             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
        end loop;   
    end loop;   
end;                 
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:处理多行查询语句                              
                                                       
*********************************************************/                     
declare  
    type empcurtyp is ref cursor;   
    emp_cv empcurtyp;   
    emp_record emp%rowtype;   
    sql_stat varchar2(100);   
begin  
    sql_stat:='select * from emp where deptno:=dno';   
   open emp_cv for sql_stat using &dno;   
    loop   
       fetch emp_cv into emp_record;   
        exit when emp_cv%notfound ;   
        dbms_output.put_line('雇员名:'||emp_record.ename||',工资:'||emp_record.sal);   
   end loop;   
   close emp_cv;   
end;   
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:使用bulk子句处理dml语句返回子句                             
                                                       
*********************************************************/       
declare      
     type ename_table_type is table of emp.ename%type   
      index by binary_integer;   
     type sal_table_type is table of emp.sal%type   
      index by binary_integer;   
       ename_table ename_table_type;   
       sal_table sal_table_type;   
       sql_stat varchar2(100);   
begin  
     sql_stat:='update emp set sal=sal*(1+:percent/100)'   
            ||'where deptno=:dno'   
            ||'returning ename,sal into :name,:salary';   
    execute immediate sql_stat using &percen ,&dno returning bulk collect into ename_table,sal_table;   
    for i in 1..ename_table.count loop   
        dbms_output.put_line('雇员:'||ename_table(i)   
              ||',的新工资为'|| sal_table(i));   
    end loop;                            
end;     
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:使用bulk子句处理多行查询                             
                                                       
*********************************************************/         
declare   
     type ename_table_type is table of emp.ename%type   
       index by binary_integer;   
     ename_table ename_table_type;   
     sql_stat varchar2(100);   
begin  
     sql_stat:='select ename from emp where deptno+:dno';   
    execute immediate sql_stat bulk collect into ename_table using &dno;   
    for i in 1..ename_table.count loop   
         dbms_output.put_line(ename_table(i));   
    end loop;       
end;   
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:在fetch语句中使用bulk子句                           
                                                       
*********************************************************/       
declare     
     type empcurtyp is ref cursor;   
     emp_cv empcurtyp ;   
     type ename_table_type is table of emp.ename%type   
       index by binary_integer;   
     ename_table ename_table_type;   
     sql_stat varchar2(100);   
begin  
     sql_stat:='select ename from emp where job:title';   
    open emp_cv for sql_stat using '&job';   
    fetch emp_cv bulk collect into ename_table;   
    for i in 1..ename_table.count loop   
       dbms_output.put_line(ename_table(i));   
    end loop;   
    close emp_cv;   
end;               
/*********************************************************   
                                                       
    Author:qinyangzhao                                  
    describe:在forall语句中使用bulk子句                           
                                                       
*********************************************************/        
declare  
     type ename_table_type is table of emp.ename%type;   
     type sal_table_type is table of emp.sal%type;   
     ename_table ename_table_type;   
     sal_table sal_table_type;   
     sql_stat varchar2(100);   
begin  
     ename_table:=name_table_type('scott','smith','clark');   
     sql_stat:='update emp set sal=sal*1.1 where ename=:1'   
            ||'returning sal into :2';   
     forall i in 1..ename_talbe.count  
        execute immediate sql_stat using ename_table(i)   
             returing bulk collect into sal_table ;   
    for j in 1..ename_table.count loop   
         dbms_output.put_line('雇员'||ename_table(j)          
             ||'的新工资为'||sal_table(j));   
    end loop;   
end;

分享到:
评论

相关推荐

    Oracle PL/SQL程序设计(第5版)(下册)第二部分

    《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...

    Oracle 12c PL/SQL程序设计终极指南

    PL/SQL本身涉及的知识点浩瀚、庞杂...当然,最为重要的还是内容本身,本书首先对PL/SQL的理论基础进行了全面的介绍,其次详细讲解PL/SQL开发的所有功能模块、方法和技巧,最后对它的各种高级特性也进行了深入探讨。

    Oracle PL/SQL程序设计(第5版)(下册) 第一部分

    《Oracle PL/SQL程序设计(第5版)》基于Oracle数据库11g,从PL/SQL编程、PL/SQL程序结构、PL/SQL程序数据、PL/SQL中的SQL、PL/SQL应用构建、高级PL/SQL主题这6个方面详细系统地讨论了PL/SQL以及如何有效地使用它。...

    精通Oracle.10g.Pl.SQL编程

    对于PL/SQL开发人员,可能已经非常熟悉PL/SQL的基本开发方法,但可能对PL/SQL高级内容(集合类型,对象类型,LOB对象处理)和oracle9i/oracle10g的PL/SQL新特性知之较少,本书对于这些高级知识和新特性有非常详尽的介绍....

    OCI_常用函数说明

    ORACLE数据库除了提供SQL和PL/SQL来访问数据库外,还提供了一个第三代程序设计语言的接口,用户可以通过C、COBOL、FORTRAN等第三代语言来编程访问数据库。OCI就是为了实现高级语言访问数据库而提供的接口。OCI允许...

    oracle数据库11G初学者指南.Oracle.Database.11g,.A.Beginner's.Guide

    5.2 基本PL/SQL编程结构 5.3 定义PL/SQL数据类型 5.3.1 有效字符集 5.3.2 算术操作符 5.3.3 varchar2类型 5.3.4 数字类型 5.3.5 日期类型 5.3.6 布尔类型 5.4 在SQL*Plus中编写PL/SQL程序, 5.4.1 PL/SQL程序中...

    Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐)--随书源代码

    该资料是《Oracle SQL高级编程》的源代码 对应的书籍资料见: Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐) 基本信息 原书名: Pro Oracle SQL 原出版社: Apress 作者: (美)Karen Morton Kerry ...

    精通Oracle10gPl.SQL編程(PDF中文)

    本书内容包括Oracle数据库的基本概念;Oracle数据库结构和实用程序;...高级SQL特性;用interMedia、基于C的外部过程、Java存储过程和对象关系特性实现Oracle数据库功能的扩展;Oracle数据库安全管理的实现方式等。

    Oracle Database 11g初学者指南--详细书签版

    5.2 基本PL/SQL编程结构 123 5.3 定义PL/SQL数据类型 124 5.3.1 有效字符集 124 5.3.2 算术操作符 125 5.3.3 varchar 2类型 126 5.3.4 数字类型 127 5.3.5 日期类型 127 5.3.6 布尔类型 128 5.4 在SQL*Plus...

    剑破冰山++Oracle开发艺术[1].part01

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part07

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part04

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part02

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part08

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part10

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part03

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part05

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

    剑破冰山++Oracle开发艺术[1].part06

    此外还有大量案例:Where In List问题解析,数据库设计和大数据量处理、数据审核、号段选取应用、分析SQL执行计划的关注点、Oracle开发误区探索、提升PL/SQL开发性能漫谈、管道函数的学习与实战应用、巧用锁特性避免...

Global site tag (gtag.js) - Google Analytics