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 閱讀(1176) 評論(0)  編輯  收藏 所屬分類: 06.PLSQL
          主站蜘蛛池模板: 沙雅县| 饶河县| 双鸭山市| 达拉特旗| 射洪县| 临安市| 林州市| 兰州市| 砀山县| 疏附县| 武穴市| 台南县| 长丰县| 榆树市| 临海市| 沂水县| 商丘市| 武冈市| 乐山市| 梅州市| 砀山县| 康平县| 安泽县| 隆子县| 利川市| 伊春市| 浪卡子县| 邹城市| 唐山市| 丽江市| 涟水县| 延川县| 上栗县| 江口县| 舒城县| 钟祥市| 多伦县| 新密市| 柘城县| 通海县| 板桥市|