Load XML File into Teradata via BTEQ

Raymond Raymond event 2020-09-14 visibility 1,771
more_vert

BTEQ is a Teradata utility tool that can be used to run Teradata SQL statements incl. DDL, DML, etc. It can also be used to import data from text file into Teradata databases. Like TPT and FASTLOAD, it can run in both batch and interactive modes.

This article demonstrates how to load XML file into Teradata.

Sample XML file

Create a sample XML file named sample.xml with the following content:

<category name="A">
	<txns>
		<txn id="1">10.</txn>
		<txn id="2">11.</txn>
		<txn id="3">12.</txn>
	</txns>
</category>

Sample table

Use the following statement to create the target table:

CREATE TABLE TestDb.BTEQ_XML_LOAD
(ID INT, XML_DOC XML) 
PRIMARY INDEX(ID);

Create a CSV index file

Create a CSV file named xml-docs.csv with the following content:

sample.xml,1
warning Note - you may encounter error 'Error: Teradata Database returned an invalid LOB token.' if the LOB (incl. XML, JSON) file name is not the first attribute. 

Create BTEQ script file 

Create BTEQ script file named load-xml.bteq with the following content:

LOGON 192.168.119.128/DBC,DBC;
DATABASE TestDb;
.IMPORT VARTEXT ',' DEFERCOLS=1 FILE='xml-docs.csv';
USING (XML_DOC XML AS DEFERRED, ID VARCHAR(10))
INSERT INTO BTEQ_XML_LOAD (CAST(:ID AS INTEGER), :XML_DOC);
LOGOFF ;

The above script reads from xml-docs.csv file and then use the first attribute as deferred.

Run the script file

Run the following command to execute above BTEQ script:

bteq < load-xml.bteq

Output:

bteq < load-xml.bteq
BTEQ 16.10.00.02 Mon Sep 14 17:02:53 2020 PID: 12352

+---------+---------+---------+---------+---------+---------+---------+----
LOGON 192.168.119.128/DBC,

 *** Logon successfully completed.
 *** Teradata Database Release is 16.10.00.03
 *** Teradata Database Version is 16.10.00.03
 *** Transaction Semantics are BTET.
 *** Session Character Set Name is 'ASCII'.

 *** Total elapsed time was 1 second.

+---------+---------+---------+---------+---------+---------+---------+----
DATABASE TestDb;

 *** New default database accepted.
 *** Total elapsed time was 1 second.


+---------+---------+---------+---------+---------+---------+---------+----
.IMPORT VARTEXT ',' DEFERCOLS=1 FILE='xml-docs.csv';
+---------+---------+---------+---------+---------+---------+---------+----
USING (XML_DOC XML AS DEFERRED, ID VARCHAR(10))
INSERT INTO BTEQ_XML_LOAD (CAST(:ID AS INTEGER), :XML_DOC);

 *** Insert completed. One row added.
 *** Total elapsed time was 1 second.


+---------+---------+---------+---------+---------+---------+---------+----
LOGOFF ;
 *** You are now logged off from the DBC.
 *** BTEQ exiting due to EOF on stdin.
 *** Exiting BTEQ...
 *** RC (return code) = 0

References

Query XML data

Refer to page Extract XML Data via SQL Functions in Teradata to find out details about how to extract XML data using native functions.

More details about BTEQ IMPORT command

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts