Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

12.5.9 Lob Conversion Functions

Oracle provides several conversion functions that are sometimes useful when working with large object data, described in Table 12-2.

Table 12-2. LOB conversion functions

Function

Description

TO_CLOB (character_data)

Converts character data into a CLOB. The input to TO_CLOB can be any of the following character types: VARCHAR2, NVARCHAR2, CHAR, NCHAR, CLOB, and NCLOB. If necessary (for example, if the input is NVARCHAR2), input data is converted from the national character set into the database character set.

TO_BLOB(raw_data)

Similar to TO_CLOB, but converts RAW or LONG RAW data into a BLOB.

TO_NCLOB (character_data)

Does the same as TO_CLOB, except that the result is an NCLOB using the national character set.

TO_LOB (long_data)

Accepts either LONG or LONG RAW data as input, and converts that data into a CLOB or a BLOB, respectively. TO_LOB may be invoked only from the SELECT list of a subquery in an INSERT...SELECT...FROM statement.

TO_RAW

Takes a BLOB as input and returns the BLOB's data as a RAW value.

The TO_LOB function is designed specifically to enable one-time conversion of LONG and LONG RAW columns into CLOB and BLOB columns, because LONG and LONG RAW are now considered obsolete. The TO_CLOB and TO_NCLOB functions provide a convenient mechanism for converting character large object data between the database and national language character sets.

12.6 Predefined Object Types

Oracle9i implements a collection of useful, predefined object types. These include:

XMLType

Use this to store and manipulate XML data.

Various URI types

Use these to store uniform resource identifiers (such as HTML addresses).

Various "Any" types

Use these to define a PL/SQL variable that can hold any type of data.

In the following sections, we provide brief introductions to these predefined object types and then point you to sources of more information.

12.6.1 The xmlType Type

XML (Extensible Markup Language) is fast becoming a very important technology to understand. Did we say "fast becoming"? Strike that—XML is important right now, and the predefined object type XMLType enables you to store XML data in an Oracle database and manipulate that XML data from within SQL and PL/SQL.

XML is a huge subject that we can't hope to cover in detail. Instead, we will settle for familiarizing you with XMLType so that you understand how important it is and what you can do with it. To learn more about using XML with Oracle, we recommend Steve Muench's book, Building Oracle XML Applications (O'Reilly). To learn more about XML in general, you might try Learning XML by Erik T. Ray (O'Reilly).

In Oracle9i Release 1, you need to use the "SYS." prefix when referencing the XMLType object type. Release 2 allows synonyms to point to object types, and the database creation script ($ORACLE_HOME/rdbms/admin/dbmsxmlt.sql) that creates XMLType now also creates the public synonym XMLTYPE, which points to the SYS.XMLType predefined object type.

Using XMLType, you can easily create a table to hold XML data:

CREATE TABLE falls (

fall_id NUMBER,

fall SYS.XMLType

);

The fall column in this table is of XMLType and can hold XML data. To store XML data into this column, you must invoke the static CreateXML method, passing it your XML data. CreateXML accepts XML data as input and instantiates a new XMLType object to hold that data.

The new object is then returned as the method's result, and it is that object that you must store in the column. CreateXML is overloaded to accept both VARCHAR2 strings and CLOBs as input.

Use the following INSERT statements to create three XML documents in the falls table:

INSERT INTO falls VALUES (1, XMLType.CreateXML(

'<?xml version="1.0"?>

<fall>

<name>Munising Falls</name>

<county>Alger</county>

<state>MI</state>

<url>

http://michiganwaterfalls.com/munising_falls/munising_falls.html

</url>

</fall>'));

INSERT INTO falls VALUES (2, XMLType.CreateXML(

'<?xml version="1.0"?>

<fall>

<name>Au Train Falls</name>

<county>Alger</county>

<state>MI</state>

<url>

http://michiganwaterfalls.com/autrain_falls/autrain_falls.html

</url>

</fall>'));

INSERT INTO falls VALUES (3, XMLType.CreateXML(

'<?xml version="1.0"?>

<fall>

<name>Laughing Whitefish Falls</name>

<county>Alger</county>

<state>MI</state>

</fall>'));

You can query XML data in the table using various XMLType methods. the existsNode method used in the following example allows you to test for the existence of a specific XML node in an XML document. The built-inSQL EXISTSNODE function, also in the example, performs the same test. Whether you use the method or the built-in function, you identify the node of interest using an XPath expression.[2]

[2] XPath is a syntax used to describe parts of an XML document. Among other things, you can use XPath to specify a particular node, or attribute value, in an XML document.

Both of the following statements produce the same output:

SQL> SELECT fall_id

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]