DBMS_ROWID包允许我们使用PL/SQL程序或SQL语句创建rowids和获取rowid的信息。例如可以通过其找到数据对象编号,数据文件编号,包含数据行的数据块编号及数据块中的数据行。该包从Oracle 8.X开始可用。
DBMS_ROWID的几个子函数程序:
1、ROWID_BLOCK_NUMBER函数(该函数返回输入ROWID的数据块编号)
语法:
DBMS_ROWID.ROWID_BLOCK_NUMBER(
row_id IN ROWID,
ts_type_in IN VARCHAR2 DEFAULT 'SMAILLFILE')
返回值是number类型。
参数:
row_id:被转换的rowid。
ts_type_in:数据行所在表空间类型,默认是SMALLFILE,即小数据文件表空间。
举例:
SQL> select dbms_rowid.rowid_block_number(rowid,'smallfile') block_id from emp where rownum <=3;
BLOCK_ID
----------
30
30
30
2、ROWID_CREATE函数(返回一个基于单独行的rowid)
语法:
DBMS_ROWID.ROWID_CREATE (
rowid_type IN NUMBER,
object_number IN NUMBER,
relative_fno IN NUMBER,
block_number IN NUMBER,
row_number IN NUMBER)
参数:
rowid_type:rowid类型(restricted或者extended)。设置rowid_type为0时,代表restricted ROWID(此时,将忽略参数object_number):设置rowid_type为1时,代表extended ROWID。
object_number:数据对象编号(仅restricted类型rowid可用)。
relative_fno:所在数据文件编号。
block_number:该数据文件中的数据块编号。
row_number:在该块中的行编号。
举例:
创建restricted ROWID
SQL> select dbms_rowid.rowid_create(0,9999,12,1000,13) from dual;
DBMS_ROWID.ROWID_C
------------------
000003E8.000D.000C
创建extended ROWID
SQL> select dbms_rowid.rowid_create(1,9999,12,1000,13) from dual;
DBMS_ROWID.ROWID_C
------------------
AAACcPAAMAAAAPoAAN
3、ROWID_INFO过程(该过程返回一个ROWID的相关信息,包括类型和ROWID的一些其他部分,注意:这时一个存储过程,不能在SQL语句中使用)
语法:
DBMS_ROWID.ROWID_INFO (
rowid_in IN ROWID, --输入参数
ts_type_in IN VARCHAR2 DEFAULT 'SMALLFILE', --输入参数
rowid_type OUT NUMBER, --out代表输出参数
object_number OUT NUMBER,
relative_fno OUT NUMBER,
block_number OUT NUMBER,
row_number OUT NUMBER);
参数:
rowid_in:ROWID to be interpreted. This determines if the ROWID is a restricted (0) or extended (1) ROWID.
ts_type_in:The type of the tablespace (bigfile/smallfile) to which the row belongs.
rowid_type:Returns type (restricted/extended).
object_number:Returns data object number (rowid_object_undefined for restricted).
relative_fno:Returns relative file number.
block_number:Returns block number in this file.
row_number:Returns row number in this block.
举例:
SQL> set serverout on
SQL> declare
2 my_rowid rowid;
3 rowid_type number;
4 object_number number;
5 relative_fno number;
6 block_number number;
7 row_number number;
8 begin
9 my_rowid :=dbms_rowid.rowid_create(1, 6877,1,23722,0);
10 dbms_rowid.rowid_info(my_rowid, rowid_type, object_number,
11 relative_fno, block_number, row_number);
12 dbms_output.put_line('ROWID: ' || my_rowid);
13 dbms_output.put_line('Object#: ' || object_number);
14 dbms_output.put_line('RelFile#: ' || relative_fno);
15 dbms_output.put_line('Block#: ' || block_number);
16 dbms_output.put_line('Row#: ' || row_number);
17 end;
18 /
ROWID: AAABrdAABAAAFyqAAA
Object#: 6877
RelFile#: 1
Block#: 23722
Row#: 0
PL/SQL procedure successfully completed.
4、ROWID_OBJECT函数(该函数返回扩展ROWID的数据对象编号,如果输入的ROWID类型为restricted,则该函数返回值为0)
语法:
DBMS_ROWID.ROWID_OBJECT (rowid_id IN ROWID)
举例:
SQL> select dbms_rowid.rowid_object(ROWID) from T1;
DBMS_ROWID.ROWID_OBJECT(ROWID)
------------------------------
12949
12949
5、ROWID_RELATIVE_FNO函数(该函数根据输入的ROWID值,返回其所在数据文件编号)
语法:
DBMS_ROWID.ROWID_RELATIVE_FNO (
rowid_id IN ROWID,
ts_type_in IN VARCHAR2 DEFAULT 'SMALLFILE')
RETURN NUMBER;
举例:
SQL> select dbms_rowid.rowid_relative_fno(ROWID) FILE# from T1;
FILE#
----------
6
6
5、ROWID_ROW_NUMBER函数(该函数根据输入的ROWID提取出行(row)号)
语法:
DBMS_ROWID.ROWID_ROW_NUMBER (row_id IN ROWID)
RETURN NUMBER;
举例:
SQL> select dbms_rowid.rowid_row_number(ROWID) ROW_NUM from T1;
ROW_NUM
----------
0
1
6、ROWID_TO_ABSOLUTE_FNO函数(从输入的ROWID中提取出其所在完全文件号)
语法:
DBMS_ROWID.ROWID_TO_ABSOLUTE_FNO (
row_id IN ROWID,
schema_name IN VARCHAR2,
object_name IN VARCHAR2)
RETURN NUMBER;
举例:
SQL> select dbms_rowid.rowid_to_absolute_fno(rowid,'LIGLE','T1') FROM T1;
DBMS_ROWID.ROWID_TO_ABSOLUTE_FNO(ROWID,'LIGLE','T1')
----------------------------------------------------
6
6
7、ROWID_TYPE函数(返回ROWID的类型,返回0代表restricted ROWID;返回1代表extended ROWID)
语法:
DBMS_ROWID.ROWID_TYPE (
rowid_id IN ROWID)
RETURN NUMBER;
举例:
SQL> select dbms_rowid.rowid_type(rowid) from t1;
DBMS_ROWID.ROWID_TYPE(ROWID)
----------------------------
1
1
还有几个函数,用时查下文档就可以了,都比较简单。