DB2 Version 9.7 for Linux, UNIX, and Windows

REC2XML scalar function

Read syntax diagramSkip visual syntax diagram
>>-REC2XML--(--decimal-constant--,--format-string--------------->

                      .----------------.      
                      V                |      
>--,--row-tag-string----,--column-name-+--)--------------------><

The schema is SYSIBM.

The REC2XML function returns a string formatted with XML tags, containing column names and column data. In a Unicode database, if a supplied argument is a graphic string, it is first converted to a character string before the function is executed.

decimal-constant
The expansion factor for replacing column data characters. The decimal value must be greater than 0.0 and less than or equal to 6.0. (SQLSTATE 42820).

The decimal-constant value is used to calculate the result length of the function. For every column with a character data type, the length attribute of the column is multiplied by this expansion factor before it is added in to the result length.

To specify no expansion, use a value of 1.0. Specifying a value less than 1.0 reduces the calculated result length. If the actual length of the result string is greater than the calculated result length of the function, then an error is raised (SQLSTATE 22001).

format-string
The string constant that specifies which format the function is to use during execution.

The format-string is case-sensitive, so the following values must be specified in uppercase to be recognized.

COLATTVAL or COLATTVAL_XML
These formats return a string with columns as attribute values.
Read syntax diagramSkip visual syntax diagram
>>-<--row-tag-string-->----------------------------------------->

   .----------------------------------------------------------------------.   
   V                                                                      |   
>----<--column-name--=--"column-name"--+->--column-value--</--column-->-+-+-->
                                       '-null="true"--/>----------------'     

>--</--row-tag-string-->---------------------------------------><

Column names may or may not be valid XML attribute values. For column names which are not valid XML attribute values, character replacement is performed on the column name before it is included in the result string.

Column values may or may not be valid XML element names. If the format-string COLATTVAL is specified, then for the column names which are not valid XML element values, character replacement is performed on the column value before it is included in the result string. If the format-string COLATTVAL_XML is specified, then character replacement is not performed on column values (although character replacement is still performed on column names).

row-tag-string
A string constant that specifies the tag used for each row. If an empty string is specified, then a value of 'row' is assumed.

If a string of one or more blank characters is specified, then no beginning row-tag-string or ending row-tag-string (including the angle bracket delimiters) will appear in the result string.

column-name
A qualified or unqualified name of a table column. The column must have one of the following data types (SQLSTATE 42815):
  • numeric (SMALLINT, INTEGER, BIGINT, DECIMAL, REAL, DOUBLE)
  • character string (CHAR, VARCHAR; a character string with a subtype of BIT DATA is not allowed)
  • datetime (DATE, TIME, TIMESTAMP)
  • a user-defined type based on one of the above types
The same column name cannot be specified more than once (SQLSTATE 42734).

The result of the function is VARCHAR. The maximum length is 32 672 bytes (SQLSTATE 54006).

Consider the following invocation:
   REC2XML (dc, fs, rt, c1, c2, ..., cn)
If the value of "fs" is either "COLATTVAL" or "COLATTVAL_XML", then the result is the same as this expression:
'<' CONCAT rt CONCAT '>' CONCAT y1 CONCAT y2
CONCAT ... CONCAT yn CONCAT '</' CONCAT rt CONCAT '>'
where yn is equivalent to:
'<column name="' CONCAT xvcn CONCAT vn
and vn is equivalent to:
'">' CONCAT rn CONCAT '</column>'
if the column is not null, and
'" null="true"/>'
if the column value is null.

xvcn is equivalent to a string representation of the column name of cn, where any characters appearing in Table 2 are replaced with the corresponding representation. This ensures that the resulting string is a valid XML attribute or element value token.

The rn is equivalent to a string representation as indicated in Table 1
Table 1. Column Values String Result
Data type of cn rn
CHAR, VARCHAR The value is a string. If the format-string does not end in the characters "_XML", then each character in cn is replaced with the corresponding replacement representation from Table 2, as indicated. The length attribute is: dc * the length attribute of cn.
SMALLINT, INTEGER, BIGINT, DECIMAL, NUMERIC, REAL, DOUBLE The value is LTRIM(RTRIM(CHAR(cn))). The length attribute is the result length of CHAR(cn). The decimal character is always the period ('.') character.
DATE The value is CHAR(cn,ISO). The length attribute is the result length of CHAR(cn,ISO).
TIME The value is CHAR(cn,JIS). The length attribute is the result length of CHAR(cn,JIS)
TIMESTAMP The value is CHAR(cn). The length attribute is the result length of CHAR(cn).

Character replacement:

Depending on the value specified for the format-string, certain characters in column names and column values will be replaced to ensure that the column names form valid XML attribute values and the column values form valid XML element values.

Table 2. Character Replacements for XML Attribute Values and Element Values
Character Replacement
< &lt;
> &gt;
" &quot;
& &amp;
' &apos;

Examples:

Note: REC2XML does not insert blank spaces or new line characters in the output. All example output has been formatted to enhance readability.