zoukankan      html  css  js  c++  java
  • %type的用法

    //%type  
    //如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量  
    //锚定到这个列上。这样做有什么好处呢?  
    //比如:  
    //declare v_ename scott.emp.ename%type;  
    //当数据类型发生变化时,此方法显得非常灵活。  
    //如果更改了列的长度,那么锚定到该列上的所有变量都会自动更改其长度;  
    //假设我们将v_ename定义为varchar2(10),那么当emp表中的ename列发生变化时,  
    //我们得手动将v_enam更改为emp.ename相同的数据长度;  
    //当我们使用锚定类型后,变量就会自动进行调整。  
    //%rowtype  
    //%rowtype与%type相似;不过它将变量锚定到表的所有列,而不是锚定到某一列;  
    //更多关于%rowtype与%type,  
    //请参考:http://blog.csdn.NET/BOBO12082119/archive/2010/12/02/6051367.aspx  
    //下面是一个实例:  
    create table dept(  
           deptno varchar2(5),  
           dname varchar2(20),  
           loc varchar2(20)  
           );  
    create or replace procedure pro_insert(  
           deptno_in in dept.deptno%type,  
           dname_in in dept.dname%type,  
           loc_in in dept.loc%type  
           )  
    as  
      v_dept dept%rowtype;  
    begin  
         begin  
              insert into dept  
              select deptno_in,dname_in,loc_in  
              from dual;  
              commit;  
              dbms_output.put_line('inserting successed');  
              exception  
              when others then  
                   rollback;  
         end;  
         begin  
              select deptno_in,dname_in,loc_in  
              into v_dept from dual;  
              dbms_output.put_line(  
              'The data having been inserted.'||  
              'deptno:'||v_dept.deptno||  
              ',dname:'||v_dept.dname||  
              ',loc:'||v_dept.loc  
              );  
         end;  
    end pro_insert;  
    //  
    //上面的过程中,使用到了嵌套块;  
    //所谓嵌套块就是块中包含其他子块;  
    //嵌套块允许出现在代码块的异常处理部分和执行部分,  
    //但是不允许出现在声明中。  
    //  
    SQL> set serveroutput on;  
    SQL> exec pro_insert('111','财务部','福州');  
    inserting successed  
    The data having been inserted.deptno:111,dname:财务部,loc:福州  
    PL/SQL procedure successfully completed  
    //  
    //从这里看出来,%rowtype定义的变量作用有点相似游标,  
    //但是我们不能将%rowtype定义的变量当做游标来使用,  
    //否则我们将会得到下面的错误:  
    //ORA-01422: exact fetch returns more than requested number of rows  
    declare v_dept dept%rowtype;  
    begin  
         select deptno,dname,loc  
         into v_dept  
         from dept;  
         dbms_output.put_line(  
              'The data having been inserted.'||  
              'deptno:'||v_dept.deptno||  
              ',dname:'||v_dept.dname||  
              ',loc:'||v_dept.loc  
              );  
    end;  
    //  
    //下面我们用游标来实现上面的操作,具体看下面的匿名块:  
    declare  
           cursor cv_dept is  
           select *  
           from dept;  
    begin  
         //变量v_dept不必我们显示声明  
         //for循环会为我们隐式的打开和关闭游标,不必我们现实的打开和关闭游标  
         for v_dept in cv_dept loop  
         dbms_output.put_line(  
              'deptno:'||v_dept.deptno||  
              ',dname:'||v_dept.dname||  
              ',loc:'||v_dept.loc  
              );  
          end loop;  
    end;  
    //  
    deptno:111,dname:财务部,loc:福州  
    deptno:120,dname:销售部,loc:大连  
    deptno:130,dname:科研部,loc:北京  
    PL/SQL procedure successfully completed  
    1. //%type  
    2. //如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量  
    3. //锚定到这个列上。这样做有什么好处呢?  
    4. //比如:  
    5. //declare v_ename scott.emp.ename%type;  
    6. //当数据类型发生变化时,此方法显得非常灵活。  
    7. //如果更改了列的长度,那么锚定到该列上的所有变量都会自动更改其长度;  
    8. //假设我们将v_ename定义为varchar2(10),那么当emp表中的ename列发生变化时,  
    9. //我们得手动将v_enam更改为emp.ename相同的数据长度;  
    10. //当我们使用锚定类型后,变量就会自动进行调整。  
    11. //%rowtype  
    12. //%rowtype与%type相似;不过它将变量锚定到表的所有列,而不是锚定到某一列;  
    13. //更多关于%rowtype与%type,  
    14. //请参考:http://blog.csdn.NET/BOBO12082119/archive/2010/12/02/6051367.aspx  
    15. //下面是一个实例:  
    16. create table dept(  
    17.        deptno varchar2(5),  
    18.        dname varchar2(20),  
    19.        loc varchar2(20)  
    20.        );  
    21. create or replace procedure pro_insert(  
    22.        deptno_in in dept.deptno%type,  
    23.        dname_in in dept.dname%type,  
    24.        loc_in in dept.loc%type  
    25.        )  
    26. as  
    27.   v_dept dept%rowtype;  
    28. begin  
    29.      begin  
    30.           insert into dept  
    31.           select deptno_in,dname_in,loc_in  
    32.           from dual;  
    33.           commit;  
    34.           dbms_output.put_line('inserting successed');  
    35.           exception  
    36.           when others then  
    37.                rollback;  
    38.      end;  
    39.      begin  
    40.           select deptno_in,dname_in,loc_in  
    41.           into v_dept from dual;  
    42.           dbms_output.put_line(  
    43.           'The data having been inserted.'||  
    44.           'deptno:'||v_dept.deptno||  
    45.           ',dname:'||v_dept.dname||  
    46.           ',loc:'||v_dept.loc  
    47.           );  
    48.      end;  
    49. end pro_insert;  
    50. //  
    51. //上面的过程中,使用到了嵌套块;  
    52. //所谓嵌套块就是块中包含其他子块;  
    53. //嵌套块允许出现在代码块的异常处理部分和执行部分,  
    54. //但是不允许出现在声明中。  
    55. //  
    56. SQL> set serveroutput on;  
    57. SQL> exec pro_insert('111','财务部','福州');  
    58. inserting successed  
    59. The data having been inserted.deptno:111,dname:财务部,loc:福州  
    60. PL/SQL procedure successfully completed  
    61. //  
    62. //从这里看出来,%rowtype定义的变量作用有点相似游标,  
    63. //但是我们不能将%rowtype定义的变量当做游标来使用,  
    64. //否则我们将会得到下面的错误:  
    65. //ORA-01422: exact fetch returns more than requested number of rows  
    66. declare v_dept dept%rowtype;  
    67. begin  
    68.      select deptno,dname,loc  
    69.      into v_dept  
    70.      from dept;  
    71.      dbms_output.put_line(  
    72.           'The data having been inserted.'||  
    73.           'deptno:'||v_dept.deptno||  
    74.           ',dname:'||v_dept.dname||  
    75.           ',loc:'||v_dept.loc  
    76.           );  
    77. end;  
    78. //  
    79. //下面我们用游标来实现上面的操作,具体看下面的匿名块:  
    80. declare  
    81.        cursor cv_dept is  
    82.        select *  
    83.        from dept;  
    84. begin  
    85.      //变量v_dept不必我们显示声明  
    86.      //for循环会为我们隐式的打开和关闭游标,不必我们现实的打开和关闭游标  
    87.      for v_dept in cv_dept loop  
    88.      dbms_output.put_line(  
    89.           'deptno:'||v_dept.deptno||  
    90.           ',dname:'||v_dept.dname||  
    91.           ',loc:'||v_dept.loc  
    92.           );  
    93.       end loop;  
    94. end;  
    95. //  
    96. deptno:111,dname:财务部,loc:福州  
    97. deptno:120,dname:销售部,loc:大连  
    98. deptno:130,dname:科研部,loc:北京  
    99. PL/SQL procedure successfully completed  

    %RowType 的使用,获取某行的数据类型。

    set serveroutput on;
    DECLARE
      rowData  student%ROWTYPE;
    BEGIN
      SELECT * INTO rowData
      FROM student
      WHERE sName = 'Jack';
     
      DBMS_OUTPUT.put_line ('find the name: '||rowData.sName);
      DBMS_OUTPUT.put_line ('find the age: '||rowData.SAGE);
      DBMS_OUTPUT.put_line ('find the email: '||rowData.sEmail);
      DBMS_OUTPUT.put_line ('find the phone: '||rowData.sPhone);
      DBMS_OUTPUT.put_line ('find the address: '||rowData.sAddress);
       
    END;
  • 相关阅读:
    caffe常用层: batchNorm层和scale层
    简述configure、pkg-config、pkg_config_path三者的关系
    python删除list中元素的三种方法
    Leetcode 872. Leaf-Similar Trees
    Leetcode 508. Most Frequent Subtree Sum
    Leetcode 572. Subtree of Another Tree
    Leetcode 894. All Possible Full Binary Trees
    Leetcode 814. Binary Tree Pruning
    Leetcode 557. Reverse Words in a String III
    python 多维list声明时的小问题
  • 原文地址:https://www.cnblogs.com/bulrush/p/6138440.html
Copyright © 2011-2022 走看看