The PL/SQL Text Book

《Oracle PL/SQL Programming》

Anchored Declarations

When you anchor a datatype,you tell PL/SQL to set the datatype of your variable based on the datatype of an already defined data structure-another PL/SQL variable,a predefined TYPE or SUBTYPE,a database table,or a specific column in a table.PL/SQL offers two kinds of anchoring:

  • Scalar anchoring:use the %TYPE attribute to define you variable based on a table`s column or some other PL/SQL scalar variabe.
  • Record anchoring:use the %ROWTYPE attribute to define your record structure based on a table or a predefined PL/SQL explicit curor.
  • The anchoring reference  is reserved at the time the code is compiled;there is no runtime overhead to anchoring.
  • The anchor also establishes a dependency between the code and the anchored element(the table,cursor,or package containing the variable referenced).Thi meas that if those elements are changed,the code in which the anchoring takes place is marked INVALID.
  • When it is recomiled ,the anchor will again be resolved,thereby keeping the code current with the anchored element.

Another %ROWTYPE Attribute Example

SET SERVEROUT ON;--创建退休表retired_emp
--创建表的时候,不能使用anchoring declaration/*
CREATE TABLE retired_emps
(empno           employees.employee_id%TYPE,ename           employees.last_name%TYPE,job             employees.job_id$TYPE,mgr             employees.manager_id%TYPE,hiredate        employees.hire_date%TYPE,leavedate       employees.hire_date$TYPE,comm            employees.commission_pct%TYPE,deptno          employees.department_id%TYPE
);
*/
/*
DROP TABLE retired_emps;CREATE TABLE retired_emps AS SELECT * FROM employees WHERE 1=2;ALTER TABLE retired_emps DROP COLUMN FIRST_NAME;
ALTER TABLE retired_emps DROP COLUMN EMAIL;
ALTER TABLE retired_emps DROP COLUMN PHONE_NUMBER;ALTER TABLE retired_emps ADD LEAVEDATE DATE;
*/
TRUNCATE TABLE retired_emps;DECLAREv_emp_id number := 124;v_emp_rec employees%ROWTYPE;
BEGINSELECT * INTO v_emp_rec FROM employees WHERE employee_id = v_emp_id;INSERT INTO retired_emps(employee_id,last_name,job_id,manager_id,hire_date,leavedate,salary,commission_pct,department_id)VALUES(v_emp_rec.employee_id,v_emp_rec.last_name,v_emp_rec.job_id,v_emp_rec.manager_id,v_emp_rec.hire_date,SYSDATE,v_emp_rec.salary,v_emp_rec.commission_pct,v_emp_rec.department_id);COMMIT;       --不能添加此查询语句,否则会报错.      --SELECT * FROM retired_emps;
END;/

DROP TABLE retired_emps;
CREATE TABLE retired_emps
(EMPNO           NUMBER(4),ENAME           VARCHAR2(10),JOB             VARCHAR2(9),MGR             NUMBER(4),HIREDATE        DATE,LEAVEDATE       DATE,SAL             NUMBER(7,2),COMM            NUMBER(7,2),DEPTNO          NUMBER(2)
);DECLAREv_employee_number NUMBER := 124;v_emp_rec         employees%ROWTYPE;
BEGINSELECT *  INTO v_emp_rec FROM employees WHERE employee_id = v_employee_number;INSERT INTO retired_emps(empno,ename,job,mgr,hiredate,leavedate,sal,comm,deptno)VALUES  (v_emp_rec.employee_id,v_emp_rec.last_name,v_emp_rec.job_id,v_emp_rec.manager_id,v_emp_rec.hire_date,SYSDATE,v_emp_rec.salary,v_emp_rec.commission_pct,v_emp_rec.department_id);COMMIT;
END;
/

Inserting a Record by Using %ROWTYPE

DECLAREv_employee_number NUMBER := 125;v_emp_rec retired_emps%ROWTYPE;
BEGINSELECT employee_id,last_name,job_id,manager_id,hire_date,SYSDATE,salary,commission_pct,department_id INTO v_emp_rec FROM employeesWHERE employee_id = v_employee_number;INSERT INTO retired_emps VALUES v_emp_rec;COMMIT;END;
/SELECT * FROM retired_emps;

Updating a Row in a Table by Using a Record

SET VERIFY OFF
DECLAREv_emp_id        NUMBER := 125;v_emp_rec       retired_emps%ROWTYPE;
BEGINSELECT * INTO v_emp_rec FROM retired_emps WHERE empno = v_emp_id;v_emp_rec.leavedate := CURRENT_DATE;UPDATE retired_emps SET ROW = v_emp_rec WHERE empno = v_emp_id;COMMIT;
END;/select empno,ename,job,mgr,to_char(hiredate,'YYYY-MM-DD HH24:MI:SS') hiredate,TO_CHAR(leavedate,'YYYY-MM-DD HH24:MI:SS') leavedate from retired_emps;

Record Level Operations

When you work at the record level,you avoid any references to individual fields in the record.Here are the record-level operations currently supported by PL/SQL:

  • You can copy the contents of one record to another,as long as they are compatible in structure.
  • You can assign a value of NULL to a record with a simple assignment.
  • You can define and pass the record as an argument in a parameter list.
  • You can RETURN  a record back through the interface of a function.
DECLAREv_emp_rec employees%ROWTYPE;
BEGINv_emp_rec := NULL;
END;
/

DROP TABLE cust_sales_roundup;CREATE TABLE cust_sales_roundup
(customer_id     NUMBER(5),customer_name   VARCHAR2(100),total_sales     NUMBER(15,2)
);DECLARE--基于表的定义Recordcust_sales_roundup_rec cust_sales_roundup%ROWTYPE;CURSOR cust_sales_cur IS SELECT * FROM cust_sales_roundup;--基于游标的定义Recordcust_sales_rec cust_sales_cur%ROWTYPE;TYPE customer_sales_rectype IS RECORD(customer_id     NUMBER(5),customer_name   departments.department_name%TYPE,total_sales     NUMBER(15,2));--基于自定义的Record
        prefererred_cust_rec customer_sales_rectype;
BEGIN-- Assign one record to anothercust_sales_roundup_rec := cust_sales_rec;prefererred_cust_rec := cust_sales_rec;
END;
/

基于Anchoring declareation技术,声明Record的不同方法(3种).

You Cannot

Several record-level operations are not yet supported:

  • You cannot use the IS NULL syntax to see if all fields in the record have NULL values.Instead,you must apply the IS NULL operator to each field individually.
  • You cannot compare two records - for example,you cannot ask if the records(the value of their fields) are same or different,or if one record is greater than or less than another.To answer these kinds of questions,you must compare each individually.

如果要比较两个record是否相等,必须通过r1.column1 == r2.column1,r1.column2 == r2.column2,...

record赋值可以赋NULL,但是不能用IS NULL来判断.

record可以当做参数传递;

转载于:https://www.cnblogs.com/arcer/archive/2013/04/21/3029932.html

[bbk5128]第12集 - Chapter 06- Working with Composite Data Types -01-4998(Record)相关推荐

  1. 生活大爆炸第6季第12集

    2019独角兽企业重金招聘Python工程师标准>>> 生活大爆炸第6季第12集 ,谢耳朵又把三个nerd给拖下水,之前就有过这种情况...不过Alex还是很漂亮的哈! 转载于:ht ...

  2. 仿美剧天蝎计划 scorpion 12集微型千斤顶机械装置

    作者:alert or einyboy 一.原由 因在追美剧天蝎计划 scorpion,在第1季12集看到了哈比做的这个装置,于是就有了这个模型的仿真.(注本模型没有给出相关材料强度方面的数值,模型不 ...

  3. 哈佛大学公开课《公平与正义》全12集115网盘下载

    哈佛大学公开课<公平与正义>全12集115网盘下载 ◎片 名 Justice What's The Right Thing To Do ◎译 名 公平与正义 ◎年 代 2009 ◎影片类型 ...

  4. SLES 12集群图形配置新界面——抢先版

      前言:各位广大的SLES忠实粉丝们,以下是SLES 12的集群管理工具的简单概述,供大家了解.对与来临的SLES 12,我们都很关注,下面由我先来个"冰山一角"吧!以下是SLE ...

  5. 越狱第三季12集出来了

    一直知道有个越狱,但是一直没看过.同学都说好看,今年回家用了一周的时间把越狱从第一季看到了第三季的十一集,十二集没出来,说要到二月12日才行,今天到tom365.com看了一下,12集出来,赶紧下载看 ...

  6. 『NLP经典项目集』06: 使用预训练模型ERNIE-GEN自动写诗

    使用PaddleNLP预训练模型ERNIE-GEN生成诗歌 诗歌,是中国文化的瑰宝,它饱含作者的思想感情与丰富的想象,语言凝练而形象性强,具有鲜明的节奏,和谐的音韵,富于音乐美.诗歌语句一般分行排列, ...

  7. 硬件大熊原创合集(2022/06更新)

    2022/05月份更新的篇章 LED模拟与数字调光 面试题:高速PCB一般布局.布线原则 硬核分享:硬件工程师常用工具包 科学的前身来源于哲学,很多人也许还记得当初物理课上亚里士多德为了验证自由落体运 ...

  8. [bbk3100]第7集 - Chapter 04 - 介绍RAC中CVU工具的使用

    阿斯顿发水电费 转载于:https://www.cnblogs.com/arcer/archive/2013/06/14/3135747.html

  9. [bbk2908]第4集 - Chapter 03 - 介绍RAC的体系结构

    艾丝凡 转载于:https://www.cnblogs.com/arcer/archive/2013/06/14/3135338.html

最新文章

  1. mysql useradd_useradd失败
  2. AI+医疗:基于模型的医疗应用大规模分析 | 腾讯AI Lab学术论坛演讲
  3. Robot Framework 使用1-环境配置及简单网站兼容性测试(转)
  4. 0330Cache Buffers chains与共享模式疑问
  5. Pytorch完成线性回归
  6. 待更新内容mongodb
  7. 吴裕雄--天生自然 JAVASCRIPT开发学习:HTML DOM 集合(Collection)
  8. EXCEL VBA编程入门四:录制宏
  9. 一图读懂3GPP R16(附思维导图下载)
  10. 彩色图像灰度化 (RGB ⇒ Gray )(RGB ⇒ YUV)(Verilog)
  11. 安卓设备如何ROOT?玩转ROOT,让你的安卓手机更强更好用
  12. 常用的免费英文电子书
  13. 浙江理工大学本科毕业答辩beamer模板
  14. CentOS7挂载光盘
  15. linux加载和卸载驱动模块出现 'XXX': device or resource busy 错误提示
  16. linux内存条故障,linux – 如何从MCE消息中找到故障内存模块?
  17. 电力电子技术(16)——直流斩波电路
  18. MVC4 AspNet MVC下的Ajax / 使用JQuery做相关的Ajax请求
  19. Panda3D学习 (1):小行星示例程序
  20. 普通壳的脱壳方法和脱壳技巧

热门文章

  1. 这 10 行比较字符串相等的代码给我整懵了,不信你也来看看!
  2. 蚂蚁面试:字符串在JVM中如何存放?
  3. Spring Boot中如何干掉过多的if else!
  4. 40亿骚扰电话拨出,6亿用户隐私泄露,央视315曝光AI黑暗面
  5. 站在吃货的角度来解释那些和微服务有关的名词
  6. 微服务实践(五):微服务的事件驱动数据管理
  7. Mybatis:基于注解形式,传入List,返回List实体
  8. 用力和应变片计算弹性模量_一种沿深度非均匀分布的残余应力测试计算方法与流程...
  9. 黑龙江省:2025年将建成5G基站11.4万个,15万数据中心机架
  10. 数据中心冷热空气流控制优化方案