Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  397 随笔 :: 33 文章 :: 29 评论 :: 0 Trackbacks
DBMS_ROWID包学习(rowid资料)
 
 
    dbms_rowid包可以通过表中各条记录的唯一rowid号,来查看这条记录的信息,例如所属object、block等等。 这个包很简单,但是对于确定当前行的信息值还是比较有用的。而dbms_rowid包的function可以在SQL中直接使用,先简单得举几个例子:
 
SQL> select a, b,
  2         dbms_rowid.rowid_block_number(rowid) b#,
  3         dbms_rowid.rowid_object(rowid) o#
  4    from t1
  5   where a = '1';
 
    A     B         B#         O#
----- ----- ---------- ----------
    1     1      22158    1182915
 
 
SQL> declare
  2  object_no integer;
  3  row_id rowid;
  4  begin
  5  select rowid into row_id from t1
  6  where a= '1';
  7  object_no := dbms_rowid.rowid_object(row_id);
  8  dbms_output.put_line('the obj. # is '|| object_no);
  9  end;
10  /
 
the obj. # is 1182915
 
PL/SQL procedure successfully completed
 
 
 
 
 
    按照惯例,下面贴一下Oracle中自带的对这个package的注释,中间加了一些我自己的注释:
 
***************************************************************************************

create or replace package sys.dbms_rowid is

  ------------

  --  OVERVIEW

  --

  --  This package provides procedures to create ROWIDs and to interpret

  --  their contents

 

  --  SECURITY

  --

  --  The execution privilege is granted to PUBLIC. Procedures in this

  --  package run under the caller security.

 

 

  ----------------------------

 

  ----------------------------

 

  --  ROWID TYPES:

  --

  --   RESTRICTED - Restricted ROWID

  --

  --   EXTENDED   - Extended ROWID

  --

  rowid_type_restricted constant integer := 0 ;

  rowid_type_extended   constant integer := 1 ;

 

  --  ROWID VERIFICATION RESULTS:

  --

  --   VALID   - Valid ROWID

  --

  --   INVALID - Invalid ROWID

  --

  rowid_is_valid   constant integer := 0 ;

  rowid_is_invalid constant integer := 1 ;

 

  --  OBJECT TYPES:

  --

  --   UNDEFINED - Object Number not defined (for restricted ROWIDs)

  --

  rowid_object_undefined constant integer := 0 ;

 

  --  ROWID CONVERSION TYPES:

  --

  --   INTERNAL - convert to/from column of ROWID type

  --

  --   EXTERNAL - convert to/from string format

  --

  rowid_convert_internal constant integer := 0 ;

  rowid_convert_external constant integer := 1 ;

 

  --  EXCEPTIONS:

  --

  -- ROWID_INVALID  - invalid rowid format

  --

  -- ROWID_BAD_BLOCK - block is beyond end of file

  --

  ROWID_INVALID exception ;

     pragma exception_init (ROWID_INVALID, - 1410 );

  ROWID_BAD_BLOCK exception ;

     pragma exception_init (ROWID_BAD_BLOCK, - 28516 );

 

  --  PROCEDURES AND FUNCTIONS:

  --

 

  --

  -- ROWID_CREATE constructs a ROWID from its constituents:

     --这个过程可以用来创建rowid进行比对,但只有oracle自己的rowid才是有效的

  -- rowid_type - type (restricted/extended)

     -- restricted为0;extended为1

  -- object_number - data object number (rowid_object_undefined for restricted)

  -- relative_fno - relative file number

     --这是file号

  -- block_number - block number in this file

  -- file_number - file number in this block

  --

  function rowid_create(rowid_type IN number ,

                        object_number IN number ,

                        relative_fno IN number ,

                         block_number IN number ,

                        row_number IN number )

                        return rowid ;

  pragma RESTRICT_REFERENCES (rowid_create, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_INFO breaks ROWID into its components and returns them:

     --这个过程几乎就是ROWID_CREATE的逆过程,返回所有属性

  -- rowid_in - ROWID to be interpreted

  -- rowid_type - type (restricted/extended)

  -- object_number - data object number (rowid_object_undefined for restricted)

  -- relative_fno - relative file number

  -- block_number - block number in this file

  -- file_number - file number in this block

  -- ts_type_in - type of tablespace which this row belongs to

  --              'BIGFILE' indicates Bigfile Tablespace

  --              'SMALLFILE' indicates Smallfile (traditional pre-10i) TS.

  --              NOTE: These two are the only allowed values for this param

  --

  procedure rowid_info( rowid_in IN rowid ,

                        rowid_type OUT number ,

                        object_number OUT number ,

                        relative_fno OUT number ,

                        block_number OUT number ,

                        row_number OUT number ,

                        ts_type_in IN varchar2 default 'SMALLFILE' );

  pragma RESTRICT_REFERENCES (rowid_info, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_TYPE returns the type of a ROWID (restricted/extended_nopart,..)

     --restricted则返回0;extended则返回1

  -- row_id - ROWID to be interpreted

  --

  function rowid_type(row_id IN rowid )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_type, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_OBJECT extracts the data object number from a ROWID.

  -- ROWID_OBJECT_UNDEFINED is returned for restricted rowids.

  --

  -- row_id - ROWID to be interpreted

  --

  function rowid_object(row_id IN rowid )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_object, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_RELATIVE_FNO extracts the relative file number from a ROWID.

     --返回对应的datafile号,这个还是还是有点用处的,因为同一张表不一定属于同一datafile

  -- row_id - ROWID to be interpreted

  -- ts_type_in - type of tablespace which this row belongs to

  --

  function rowid_relative_fno(row_id IN rowid ,

                              ts_type_in IN varchar2 default 'SMALLFILE' )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_relative_fno, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_BLOCK_NUMBER extracts the block number from a ROWID.

  --

  -- row_id - ROWID to be interpreted

  -- ts_type_in - type of tablespace which this row belongs to

     -- ts_type_in只有2种选择,'SMALLFILE'和'BIGFILE'

  --

  function rowid_block_number(row_id IN rowid ,

                              ts_type_in IN varchar2 default 'SMALLFILE' )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_block_number, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_ROW_NUMBER extracts the row number from a ROWID.

     --这个函数返回该条记录在block中的相对位置,大小不一定的,也不一定连续(如果删除过数据)

  -- row_id - ROWID to be interpreted

  --

  function rowid_row_number(row_id IN rowid )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_row_number, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_TO_ABSOLUTE_FNO extracts the relative file number from a ROWID,

  -- which addresses a row in a given table

     --用于标注准确文件号,多用于分区表

  -- row_id - ROWID to be interpreted

  --

  -- schema_name - name of the schema which contains the table

  --

  -- object_name - table name

  --

  function rowid_to_absolute_fno(row_id IN rowid ,

                                 schema_name IN varchar2 ,

                                 object_name IN varchar2 )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_to_absolute_fno, WNDS , WNPS , RNPS );

 

  --

  -- ROWID_TO_EXTENDED translates the restricted ROWID which addresses

  -- a row in a given table to the extended format. Later, it may be removed

  -- from this package into a different place

  --

  -- old_rowid - ROWID to be converted

  --

  -- schema_name - name of the schema which contains the table (OPTIONAL)

  --

  -- object_name - table name (OPTIONAL)

  --

  -- conversion_type - rowid_convert_internal/external_convert_external

  --                   (whether old_rowid was stored in a column of ROWID

  --                    type, or the character string)

     --rowid_convert_internal (:=0)
     --rowid_convert_external (:=1)

  function rowid_to_extended(old_rowid IN rowid ,

                             schema_name IN varchar2 ,

                             object_name IN varchar2 ,

                             conversion_type IN integer )

                        return rowid ;

  pragma RESTRICT_REFERENCES (rowid_to_extended, WNDS , WNPS , RNPS );

 

  --

  -- ROWID_TO_RESTRICTED translates the extnded ROWID into a restricted format

  --

  -- old_rowid - ROWID to be converted

  --

  -- conversion_type - internal/external (IN)

  --

  -- conversion_type - rowid_convert_internal/external_convert_external

  --                   (whether returned rowid will be stored in a column of

  --                    ROWID type, or the character string)

  --

  function rowid_to_restricted(old_rowid IN rowid ,

                               conversion_type IN integer )

                        return rowid ;

  pragma RESTRICT_REFERENCES (rowid_to_restricted, WNDS , RNDS , WNPS , RNPS );

 

  --

  -- ROWID_VERIFY verifies the ROWID. It returns rowid_valid or rowid_invalid

  -- value depending on whether a given ROWID is valid or not.

     --这个函数是用于检验是否可以讲rowid从restricted改为extended的

     --可以修改则返回0;不可以则返回1

  -- rowid_in - ROWID to be verified

  --

  -- schema_name - name of the schema which contains the table

  --

  -- object_name - table name

  --

  -- conversion_type - rowid_convert_internal/external_convert_external

  --                   (whether old_rowid was stored in a column of ROWID

  --                    type, or the character string)

  --

  function rowid_verify(rowid_in IN rowid ,

                        schema_name IN varchar2 ,

                        object_name IN varchar2 ,

                        conversion_type IN integer )

                        return number ;

  pragma RESTRICT_REFERENCES (rowid_verify, WNDS , WNPS , RNPS );

 

end ;

***************************************************************************************

 

 

    总结一下:

 

    1、ROWID_CREATE:输入相应信息后自己创建一个ROWID并返回,主要用于测试比对
    2、ROWID_INFO:返回ROWID确定的各种信息

    3、ROWID_TYPE:返回ROWID类型(restricted or extended)
    4、ROWID_OBJECT:返回该ROWID对应的OBJECT的OBJ#
    5、ROWID_RELATIVE_FNO:返回该ROWID对应的对应文件号
    6、ROWID_BLOCK_NUMBER:返回该ROWID所在的BLOCK号
    7、ROWID_ROW_NUMBER:返回该行数据在BLOCK中的相对位置
    8、ROWID_TO_ABSOLUTE_FNO:返回相关的完全数据文件号
    9、ROWID_TO_EXTENDED:将restricted类型的ROWID修改为extended
    10、ROWID_TO_RESTRICTED:将extended类型的ROWID修改为restricted
    11、ROWID_VERIFY:查看是否可以对ROWID的类型进行修改

 

    这个包的应用也比较简单,没什么可说的了,再转个eygle的文章,也是比较简单的应用的:

    -----------------------

    http://www.eygle.com/archives/2004/12/dbms_rowid_get_rowid_detail.html

 

 

posted on 2009-06-07 22:19 decode360 阅读(1167) 评论(0)  编辑  收藏 所属分类: 06.PLSQL

只有注册用户登录后才能发表评论。


网站导航: