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.

CHAPTER 12 ■ DATATYPES 519<br />

We are ready to bind the inputs to this query. Any non-NULL names that were passed to us<br />

will be “bound” to the query, so when we execute it, it finds the right row:<br />

55 bind_variable( p_name1, p_bind1 );<br />

56 bind_variable( p_name2, p_bind2 );<br />

57 bind_variable( p_name3, p_bind3 );<br />

58 bind_variable( p_name4, p_bind4 );<br />

59<br />

And now we can execute the query <strong>and</strong> fetch the row. Then using DBMS_SQL.COLUMN_<br />

VALUE_LONG, we extract the necessary substring of the LONG <strong>and</strong> return it:<br />

60 dbms_sql.define_column_long(g_cursor, 1);<br />

61 if (dbms_sql.execute_<strong>and</strong>_fetch(g_cursor)>0)<br />

62 then<br />

63 dbms_sql.column_value_long<br />

64 (g_cursor, 1, p_for, p_from-1,<br />

65 l_buffer, l_buffer_len );<br />

66 end if;<br />

67 return l_buffer;<br />

68 end substr_of;<br />

69<br />

70 end;<br />

71 /<br />

Package body created.<br />

That’s it—you should be able to use that package against any legacy LONG column in<br />

your database, allowing you to perform many WHERE clause operations that were not possible<br />

before. For example, you can now find all partitions in your schema such that the HIGH_VALUE<br />

has the year 2003 in it:<br />

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

2 from (<br />

3 select table_owner, table_name, partition_name,<br />

4 long_help.substr_of<br />

5 ( 'select high_value<br />

6 from all_tab_partitions<br />

7 where table_owner = :o<br />

8 <strong>and</strong> table_name = :n<br />

9 <strong>and</strong> partition_name = :p',<br />

10 1, 4000,<br />

11 'o', table_owner,<br />

12 'n', table_name,<br />

13 'p', partition_name ) high_value<br />

14 from all_tab_partitions<br />

15 where table_name = 'T'<br />

16 <strong>and</strong> table_owner = user<br />

17 )<br />

18 where high_value like '%2003%'

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

Saved successfully!

Ooh no, something went wrong!