Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 12 ■ DATATYPES 515 Coping with Legacy LONG Types A question that arises frequently is, “What about the data dictionary in Oracle?” It is littered with LONG columns, and this makes using the dictionary columns problematic. For example, it is not possible using SQL to search the ALL_VIEWS dictionary view to find all views that contain the text HELLO: ops$tkyte@ORA10G> select * 2 from all_views 3 where text like '%HELLO%'; where text like '%HELLO%' * ERROR at line 3: ORA-00932: inconsistent datatypes: expected NUMBER got LONG This issue is not limited to the ALL_VIEWS view—many views are affected: ops$tkyte@ORA10G> select table_name, column_name 2 from dba_tab_columns 3 where data_type in ( 'LONG', 'LONG RAW' ) 4 and owner = 'SYS' 5 and table_name like 'DBA%'; TABLE_NAME COLUMN_NAME ------------------------------ ------------------------------ DBA_VIEWS TEXT DBA_TRIGGERS TRIGGER_BODY DBA_TAB_SUBPARTITIONS HIGH_VALUE DBA_TAB_PARTITIONS HIGH_VALUE DBA_TAB_COLUMNS DATA_DEFAULT DBA_TAB_COLS DATA_DEFAULT DBA_SUMMARY_AGGREGATES MEASURE DBA_SUMMARIES QUERY DBA_SUBPARTITION_TEMPLATES HIGH_BOUND DBA_SQLTUNE_PLANS OTHER DBA_SNAPSHOTS QUERY DBA_REGISTERED_SNAPSHOTS QUERY_TXT DBA_REGISTERED_MVIEWS QUERY_TXT DBA_OUTLINES SQL_TEXT DBA_NESTED_TABLE_COLS DATA_DEFAULT DBA_MVIEW_ANALYSIS QUERY DBA_MVIEW_AGGREGATES MEASURE DBA_MVIEWS QUERY DBA_IND_SUBPARTITIONS HIGH_VALUE DBA_IND_PARTITIONS HIGH_VALUE DBA_IND_EXPRESSIONS COLUMN_EXPRESSION DBA_CONSTRAINTS SEARCH_CONDITION DBA_CLUSTER_HASH_EXPRESSIONS HASH_EXPRESSION

516 CHAPTER 12 ■ DATATYPES So, what is the solution? If you want to make use of these columns in SQL, then you’ll need to convert them to a SQL-friendly type. You can use a user-defined function for doing so. The following example demonstrates how to accomplish this using a LONG SUBSTR function that will allow you to effectively convert any 4,000 bytes of a LONG type into a VARCHAR2, for use with SQL. When you are done, you’ll be able to query: ops$tkyte@ORA10G> select * 2 from ( 3 select owner, view_name, 4 long_help.substr_of( 'select text 5 from dba_views 6 where owner = :owner 7 and view_name = :view_name', 8 1, 4000, 9 'owner', owner, 10 'view_name', view_name ) substr_of_view_text 11 from dba_views 12 where owner = user 13 ) 14 where upper(substr_of_view_text) like '%INNER%' 15 / You’ve converted the first 4,000 bytes of the VIEW_TEXT column from LONG to VARCHAR2 and can now use a predicate on it. Using the same technique, you could implement your own INSTR, LIKE, and so forth for LONG types as well. In this book, I’ll only demonstrate how to get the substring of a LONG type. The package we will implement has the following specification: ops$tkyte@ORA10G> create or replace package long_help 2 authid current_user 3 as 4 function substr_of 5 ( p_query in varchar2, 6 p_from in number, 7 p_for in number, 8 p_name1 in varchar2 default NULL, 9 p_bind1 in varchar2 default NULL, 10 p_name2 in varchar2 default NULL, 11 p_bind2 in varchar2 default NULL, 12 p_name3 in varchar2 default NULL, 13 p_bind3 in varchar2 default NULL, 14 p_name4 in varchar2 default NULL, 15 p_bind4 in varchar2 default NULL ) 16 return varchar2; 17 end; 18 / Package created.

CHAPTER 12 ■ DATATYPES 515<br />

Coping with Legacy LONG Types<br />

A question that arises frequently is, “What about the data dictionary in <strong>Oracle</strong>?” It is littered<br />

with LONG columns, <strong>and</strong> this makes using the dictionary columns problematic. For example, it<br />

is not possible using SQL to search the ALL_VIEWS dictionary view to find all views that contain<br />

the text HELLO:<br />

ops$tkyte@ORA10G> select *<br />

2 from all_views<br />

3 where text like '%HELLO%';<br />

where text like '%HELLO%'<br />

*<br />

ERROR at line 3:<br />

ORA-00932: inconsistent datatypes: expected NUMBER got LONG<br />

This issue is not limited to the ALL_VIEWS view—many views are affected:<br />

ops$tkyte@ORA10G> select table_name, column_name<br />

2 from dba_tab_columns<br />

3 where data_type in ( 'LONG', 'LONG RAW' )<br />

4 <strong>and</strong> owner = 'SYS'<br />

5 <strong>and</strong> table_name like 'DBA%';<br />

TABLE_NAME<br />

COLUMN_NAME<br />

------------------------------ ------------------------------<br />

DBA_VIEWS<br />

TEXT<br />

DBA_TRIGGERS<br />

TRIGGER_BODY<br />

DBA_TAB_SUBPARTITIONS<br />

HIGH_VALUE<br />

DBA_TAB_PARTITIONS<br />

HIGH_VALUE<br />

DBA_TAB_COLUMNS<br />

DATA_DEFAULT<br />

DBA_TAB_COLS<br />

DATA_DEFAULT<br />

DBA_SUMMARY_AGGREGATES MEASURE<br />

DBA_SUMMARIES<br />

QUERY<br />

DBA_SUBPARTITION_TEMPLATES HIGH_BOUND<br />

DBA_SQLTUNE_PLANS<br />

OTHER<br />

DBA_SNAPSHOTS<br />

QUERY<br />

DBA_REGISTERED_SNAPSHOTS QUERY_TXT<br />

DBA_REGISTERED_MVIEWS<br />

QUERY_TXT<br />

DBA_OUTLINES<br />

SQL_TEXT<br />

DBA_NESTED_TABLE_COLS<br />

DATA_DEFAULT<br />

DBA_MVIEW_ANALYSIS<br />

QUERY<br />

DBA_MVIEW_AGGREGATES<br />

MEASURE<br />

DBA_MVIEWS<br />

QUERY<br />

DBA_IND_SUBPARTITIONS<br />

HIGH_VALUE<br />

DBA_IND_PARTITIONS<br />

HIGH_VALUE<br />

DBA_IND_EXPRESSIONS<br />

COLUMN_EXPRESSION<br />

DBA_CONSTRAINTS<br />

SEARCH_CONDITION<br />

DBA_CLUSTER_HASH_EXPRESSIONS HASH_EXPRESSION

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!