05.11.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

516<br />

CHAPTER 12 ■ DATATYPES<br />

So, what is the solution? If you want to make use of these columns in SQL, then you’ll<br />

need to convert them to a SQL-friendly type. You can use a user-defined function for doing so.<br />

The following example demonstrates how to accomplish this using a LONG SUBSTR function<br />

that will allow you to effectively convert any 4,000 bytes of a LONG type into a VARCHAR2, for use<br />

with SQL. When you are done, you’ll be able to query:<br />

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

2 from (<br />

3 select owner, view_name,<br />

4 long_help.substr_of( 'select text<br />

5 from dba_views<br />

6 where owner = :owner<br />

7 <strong>and</strong> view_name = :view_name',<br />

8 1, 4000,<br />

9 'owner', owner,<br />

10 'view_name', view_name ) substr_of_view_text<br />

11 from dba_views<br />

12 where owner = user<br />

13 )<br />

14 where upper(substr_of_view_text) like '%INNER%'<br />

15 /<br />

You’ve converted the first 4,000 bytes of the VIEW_TEXT column from LONG to VARCHAR2<br />

<strong>and</strong> can now use a predicate on it. Using the same technique, you could implement your own<br />

INSTR, LIKE, <strong>and</strong> so forth for LONG types as well. In this book, I’ll only demonstrate how to get<br />

the substring of a LONG type.<br />

The package we will implement has the following specification:<br />

ops$tkyte@ORA10G> create or replace package long_help<br />

2 authid current_user<br />

3 as<br />

4 function substr_of<br />

5 ( p_query in varchar2,<br />

6 p_from in number,<br />

7 p_for in number,<br />

8 p_name1 in varchar2 default NULL,<br />

9 p_bind1 in varchar2 default NULL,<br />

10 p_name2 in varchar2 default NULL,<br />

11 p_bind2 in varchar2 default NULL,<br />

12 p_name3 in varchar2 default NULL,<br />

13 p_bind3 in varchar2 default NULL,<br />

14 p_name4 in varchar2 default NULL,<br />

15 p_bind4 in varchar2 default NULL )<br />

16 return varchar2;<br />

17 end;<br />

18 /<br />

Package created.

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

Saved successfully!

Ooh no, something went wrong!