例如,可以使用它来声明ARTIST表中字段类型相同的变量,注意可以在表中名和字段之间使用点号(.)。
DEALARE
V_UNIT_NAME kwmis_xm_unit_bg.UNIT_NAME%TYPE;
V_BGOID kwmis_xm_unit_bg.BGOID%TYPE;
通过这种方式就不需要明确指定SQL数据类型,从而改善了代码的可维护性,因为表中字段类型的改变会自动反映在PL/SQL中;
What are % TYPE and % ROWTYPE ? What are the advantages of using these over datatypes?
% TYPE provides the data type of a variable or a database column to that variable.
% ROWTYPE provides the record type that represents a entire row of a table or view or
columns selected in the cursor.
The advantages are :
i. Need not know about variable's data type
ii. If the database definition of a column in a table changes, the data type of a
variable changes accordingly.
不必知道变量的类型
数据中表的字段的定义发生变化,变量的类型也会随之自动发生变化,不需要人为改动。
%ROWTYPE
使用%ROWTYPE属性可以声明一个基于表或游标的行对象,也称为记录对象,例如,可以基于ARTIST声明一个行对象:
declare
newXmUnitBg kwmis_xm_unit_bg%rowtype;
一旦生成这条记录,那么就可以通过游标使用句号来提取newXmUnitBg的字段值,例如newXmUnitBg.UNIT_NAME 单位名称, newXmUnitBg.BGOID 变更申请号
cursor frdm_cursor is SELECT FRDM_TZH FROM kwmis_xm_unit_bg WHERE bgoid=V_BGOID and bg_flag='1'
AND FRDM_TZH in(select frdm from kwmis_unit_baseinfo);
begin
OPEN frdm_cursor;
LOOP
FETCH frdm_cursor INTO v_frdm;
if frdm_cursor%notfound then frdm_cursor%found
exit when frdm_cursor%notfound frdm_cursor%found
%TYPE是跟表中的字段的类型一致,是变量类型.而%ROWTYPE是跟表中的行类型一致,是record类型.
转自:http://www.cnblogs.com/cuihongyu3503319/archive/2007/11/28/975188.html