plsql记录,对象序列

4. PLSQL的记录类型

4.1 创建记录类型

记录是一种复合的数据类型,一种有逻辑上相关联的独立元素组成的复合的数据类型
记录只能存储一行数据,行变量是记录的一种特殊形式,有继承的原结构(显式游标,表)

(1)直接访问记录的元素,分别给每个字段赋值
declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  v_example_rec hrc_org_rec;
begin
  v_example_rec.hrc_org_id:=1001;
  v_example_rec.hrc_descr:=‘CEO/COO‘;
  v_example_rec.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
  dbms_output.put_line(to_char(v_example_rec.hrc_org_id)||‘ ‘||v_example_rec.hrc_descr||‘  ‘||
  v_example_rec.org_short_name);
exception when others then
  null;
end;

(2)记录之间的赋值
declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  v_example_rec1 hrc_org_rec;
  v_example_rec2 hrc_org_rec;
begin
  v_example_rec1.hrc_org_id:=1001;
  v_example_rec1.hrc_descr:=‘CEO/COO‘;
  v_example_rec1.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
  v_example_rec2:=v_example_rec1;
  dbms_output.put_line(to_char(v_example_rec2.hrc_org_id)||‘ ‘||v_example_rec2.hrc_descr||‘  ‘||
  v_example_rec2.org_short_name);
exception when others then
  null;
end;

将上面的练习使用记录的方式实现:

declare
  type rc is ref cursor;
  v_rc rc;
  v_deptno number;
  type emp_dept_rec is record(v_dname varchar2(30),v_empno number,v_ename varchar2(30),v_job varchar2(30),v_hiredate date,v_sal number);
  v_example_rec emp_dept_rec;
  type emp_dept_rec1 is record(v_empno number,v_sal number,v_grade number);
  v_example_rec1 emp_dept_rec1;

begin
  open v_rc for select d.dname,e.empno,e.ename,e.job,e.hiredate,e.sal
                  from emp e,dept d
                 where e.deptno=d.deptno;
  loop
    fetch v_rc into v_example_rec;      --必须要整一行都赋予记录
    exit when(v_rc%notfound);
    dbms_output.put_line(v_example_rec.v_dname||‘ ‘||v_example_rec.v_empno||‘  ‘||v_example_rec.v_ename||‘  ‘||v_example_rec.v_job||‘ ‘||to_char(v_example_rec.v_hiredate)||‘  ‘||v_example_rec.v_sal);     
  end loop;
  close v_rc;
  dbms_output.put_line(‘------------------------------------‘);
 
  open v_rc for select a.empno,a.sal,s.grade
                  from emp a,salgrade s
                 where a.sal between s.losal and s.hisal;
  loop
    fetch v_rc into v_example_rec1;
    exit when(v_rc%notfound);
    dbms_output.put_line(to_char(v_example_rec1.v_empno)||‘   ‘||rpad(to_char(v_example_rec1.v_sal),10,‘ ‘)||‘  ‘||to_char(v_example_rec1.v_grade));
  end loop;
  close v_rc;
exception when others then
  null;
end;

###########################################################################################

4.2 oracle的序列对象

(1) 定义一个序列:
create sequence hrc_org_seq minvalue 1 maxvalue 999999 start with 100 increment by 1;

(2) 查询验证:
select * from user_objects where object_name=‘HRC_ORG_SEQ‘;

select * from user_sequences;

(3) 使用方法:
SQL> select hrc_org_seq.nextval from dual;
   NEXTVAL
----------
       100

SQL> select hrc_org_seq.nextval from dual;
   NEXTVAL
----------
       101

SQL> select hrc_org_seq.nextval from dual;
   NEXTVAL
----------
       102
经常用作主键的生成器,比如:
SQL> select ‘44522‘||to_char(sysdate,‘yyyymmdd‘)||‘BOSS‘||hrc_org_seq.nextval from dual;

这个主键永远都不会重复

一般不用有业务含义的字段作主键,设计系统的时候要求一旦主键生成就不能再被修改,只能删除,也不能够update
有业务含义的字段经常会被修改,比如某个人手机号,证书号码,属性信息可能会发生错乱而导致数据的不一致。

(4)删除一个序列对象
SQL> drop sequence hrc_org_seq;
Sequence dropped

###########################################################################################

4.3记录类型的相关操作

(1) 通过select into 语句来给记录赋值

declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  v_example_rec hrc_org_rec;
begin
  select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
    into v_example_rec
    from org_tab o,hrc_tab h
   where o.hrc_code=h.hrc_code
     and o.org_id=1002;    --使用select into方式填充记录,必须要确保查到的数据是一行
  dbms_output.put_line(to_char(v_example_rec.hrc_org_id)||‘ ‘||v_example_rec.hrc_descr||‘ ‘||
  v_example_rec.org_short_name);
exception when others then
  null;
end;

(2)将6个org_id都通过记录读取并且输出,修改上面的程序

declare
  type hrc_org_rec is record(
    hrc_org_id     number,
    hrc_descr      varchar2(20),
    org_short_name varchar2(30));
  v_example_rec hrc_org_rec;
begin
  for idx in (select org_id from org_tab) loop
    select hrc_org_seq.nextval, h.hrc_descr, o.org_short_name
      into v_example_rec
      from org_tab o, hrc_tab h
     where o.hrc_code = h.hrc_code
       and o.org_id = idx.org_id;       --使用select into方式填充记录,必须要确保查到的数据是一行
    dbms_output.put_line(to_char(v_example_rec.hrc_org_id) || ‘ ‘ ||
                         v_example_rec.hrc_descr || ‘ ‘ ||
                         v_example_rec.org_short_name);
  end loop;
exception
  when others then
    null;
end;

注意:测试两个记录是否相等,不能 if record1 = record2 .... 错误的写法,只能使用每个字段的内容都去比较

declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  v_example_rec1 hrc_org_rec;
  v_example_rec2 hrc_org_rec;
begin
  v_example_rec1.hrc_org_id:=1001;
  v_example_rec1.hrc_descr:=‘CEO/COO‘;
  v_example_rec1.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
  v_example_rec2:=v_example_rec1;
  if(v_example_rec1.hrc_org_id=v_example_rec2.hrc_org_id and v_example_rec1.hrc_descr=v_example_rec2.hrc_descr and v_example_rec1.org_short_name = v_example_rec2.org_short_name) then
    dbms_output.put_line(‘YES‘);
  end if;
exception when others then
  null;
end;

练习:修改之前的练习,尝试用记录实现接收取到的值
-------------------------------------------------
 定义一个游标变量类型,声明游标,分别打开两个查询并输出
 1.员工的姓名、薪资、雇佣日期、部门名           
 2.员工的编号、薪水、薪水等级,关联salgrade表

(3) 行变量的记录:行变量是一种特殊的记录,结构是基于表或者游标的。

declare
  cursor csr_hrc is select * from hrc_tab order by 1;
  hrc_rec csr_hrc%rowtype;
  hrc_rec1 hrc_tab%rowtype;
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
  hrc_rec2 hrc_org_rec;
begin
  open csr_hrc;
  loop
     fetch csr_hrc into hrc_rec;
     exit when(csr_hrc%notfound);
     dbms_output.put_line(to_char(hrc_rec.hrc_code)||‘  ‘||hrc_rec.hrc_descr);
  end loop;
  close csr_hrc;
  dbms_output.put_line(‘-----------------------------------‘);
  open csr_hrc;
  loop
     fetch csr_hrc into hrc_rec1;
     exit when(csr_hrc%notfound);
     dbms_output.put_line(to_char(hrc_rec1.hrc_code)||‘  ‘||hrc_rec1.hrc_descr);
  end loop;
  close csr_hrc;
  dbms_output.put_line(‘-----------------------------------‘);
  open csr_hrc;
  loop
     fetch csr_hrc into hrc_rec2;
     exit when(csr_hrc%notfound);
     dbms_output.put_line(to_char(hrc_rec2.hrc_code)||‘  ‘||hrc_rec2.hrc_descr);
  end loop;
  close csr_hrc;
  dbms_output.put_line(‘-----------------------------------‘);
  open csr_hrc;
  loop
     fetch csr_hrc into hrc_rec;   --fetch值给行变量
     exit when(csr_hrc%notfound);
     hrc_rec2:=hrc_rec;            --将行变量赋值给记录类型的变量,能够赋值成功
     dbms_output.put_line(to_char(hrc_rec2.hrc_code)||‘  ‘||hrc_rec2.hrc_descr);
  end loop;
  close csr_hrc;
exception when others then
  null;
end;

(4)涉及到DML操作的记录

A insert操作

declare
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
  v_example_rec hrc_org_rec;
begin
  v_example_rec.hrc_code:=6;
  v_example_rec.hrc_descr:=‘Wev analyze l‘;
  insert into hrc_tab values v_example_rec;        --使用记录的方式插入一行数据
  commit;
exception when others then
  null;
end;

验证:
select * from hrc_tab;

B update操作

declare
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
  v_example_rec hrc_org_rec;
begin
  v_example_rec.hrc_code:=6;
  v_example_rec.hrc_descr:=‘Wev analyze 2‘;
  update hrc_tab set row=v_example_rec where hrc_code=v_example_rec.hrc_code;   --使用记录的方式更新一行数据
  commit;                                                                       --注意row关键字
exception when others then
  null;
end;

验证:
select * from hrc_tab;

C delete操作

declare
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
  v_example_rec hrc_org_rec;
begin
  v_example_rec.hrc_code:=6;
  delete from hrc_tab where hrc_code=v_example_rec.hrc_code;            --使用记录的方式删除一行记录
  commit;
exception when others then
  null;
end;

验证:
select * from hrc_tab;

注意:不能用整行作为过滤条件

declare
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
  v_example_rec hrc_org_rec;
begin
  v_example_rec.hrc_code:=6;
  v_example_rec.hrc_descr:=‘Web anayled 1‘;
  delete from hrc_tab where row=v_example_rec;      --不能使用这种方式删除
  commit;
exception when others then
  null;
end;

时间: 2024-08-10 12:02:32

plsql记录,对象序列的相关文章

JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象 。

JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对象.要添加System.Web.Extensions.dll的引用.该类位于System.Web.Script.Serialization命名空间下. 一.属性 MaxJsonLength 获取或设置 JavaScriptSerializer 类接受的 JSON 字符串的最大长度. Recursio

模式对象----序列、同义词、视图

今天学的模式对象,感觉学的不是太好,趁着自习时间来梳理一遍,以便以后查阅使用 1.模式对象---序列(sequence) 在创建序列之前必须先在system中给对象exam设置sequence模式 grant create sequence  to exam; 创建序列: -- Create sequence            create sequence seq_course   minvalue 1 ---最小值 maxvalue 9999999999999999999  ---最大值

JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象

JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对象.要添加System.Web.Extensions.dll的引用.该类位于System.Web.Script.Serialization命名空间下. 一.属性 MaxJsonLength 获取或设置 JavaScriptSerializer 类接受的 JSON 字符串的最大长度. Recursio

PLSQL记录与ROWTYPE类型

PLSQL记录类似语言中的结构struct,方便于处理单行多列数据,PLSQL记录是由一组相关的记录成员Field组成,使用的话基本上就两种类型: 一.自定义的也就是自定义Field及类型,这种一般满足于单行多列不是全部的列 TYPE EMP_RECORD_TYPE1 IS RECORD(自定义的Field V_NAME VARCHAR2(20), V_SAL NUMBER(6,3) ); EMP_RECORD1 EMP_RECORD_TYPE1; TYPE EMP_RECORD_TYPE2 I

C#对象序列化成XML,以及自定义标签名

C#对象序列化操作: public class XMLHelper { /// <summary> /// 对象序列化成 XML String /// </summary> public static string XmlSerialize<T>(T obj) { string xmlString = string.Empty; XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); //using (Te

Json数据序列化对象,及对象序列化为Json格式

public class JsonHelper { /// <summary> /// 生成Json格式 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string GetJso

对象序列和反序列化Xml

1. XmlArray和XmlArrayItem XmlArray和XmlArrayItem是不同的,XmlArray是指这个数组叫什么,XmlArrayItem 值数组的每个元素叫什么. <XmlArray> <XmlArrayItem>0</XmlArrayItem> <XmlArrayItem>1</XmlArrayItem> <XmlArrayItem>2</XmlArrayItem> </XmlArray&

jvm学习记录-对象的创建、对象的内存布局、对象的访问定位

简述 今天继续写<深入理解java虚拟机>的对象创建的理解.这次和上次隔的时间有些长,是因为有些东西确实不好理解,就查阅各种资料,然后弄明白了才来做记录. (此文中所阐述的内容都是以HotSpot虚拟机为例的.) 对象的创建 java程序在运行过程中无时无刻都有对象被创建出来,那么创建对象是个怎么样的过程呢?还是看看我自己的理解吧. 判断是否已经执行类加载 当虚拟机遇到一条new指令时 ,首先去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载

java记录对象前后修改的内容(工具类)

有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属性上面加上注解,value设置为属性的中文描述 工具了代码如下 util类(BeanChangeUtil) 1 import java.beans.PropertyDescriptor; 2 import java.lang.reflect.Field; 3 import java.lang.ref