Loads character strings as records in the specified table. The records to be inserted can be indicated in the document itself within a variable string, or by referring to an absolute file with the full path.

When the process starts, a transaction opens and when the list of records is finished, it closes. If an error occurs with any of the registers, the system performs a reverse (rollback) and therefore no register is inserted.

Temporary tables with an automatically generated name are also supported (name specified with the "@" symbol).

1 table.load

<table.load
    columns='columns'
    colindexes='colindexes'
    colwidths='colwidths'
    date-format='date-format'
    decimal-format='decimal-format'
    delimiter='delimiter'
    autocommit='autocommit'
    encoding='encoding'
    enclosing-char='enclosing-char'
    gzip='gzip'
    firstRowIsHeader='firstRowIsHeader'
    replace='replace'
    primary-key-columns='primary-key-columns'
    debug='debug'
    table='table'
    counter-step='counter-step'
    counter-max='counter-max'
    encoding='encoding'
>
    <rows /> ?
    <instructions> ?
        <filter> ?
            <column name='name' /> +
        </filter>
        <onError> ?
            <instructions /> ?
        </onError>
    </instructions>
    <data count='count'> ?
        <rows /> ?
    </data>
</table.load>

Exceptions

requires 1 arguments, received: [...]

Wrong number of arguments specified.

incorrect number of columns at line 1, [...] (table) != [...] (row)

An incorrect number of values has been indicated with respect to the number of columns in the table. This could also occur if there is a line break at the beginning of the load file.

expected <data> while found <...>

Formatting functions ( filters ) have been indicated and the values to be loaded are not within the data tag.

Delimiter specified '...' is incorrect. It should be single character.

A character code not considered as a delimiter is used.

Remarks

When the process starts, a transaction opens and when the list of records is finished, it closes. If any error occurs with any of the records, the system performs a reverse ( rollback ) and therefore no record is inserted.

Example

Load records contained in a string into a table..

Copy
<xsql-script name='table_load_sample1'>
    <body>
        <table name='persones'>
            <column name='dni'     type='char'    size='9'  />
            <column name='nom'     type='varchar' size='20' />
            <column name='cognom'  type='varchar' size='25' />
        </table>

        <table.load table='persones'>
               <!-- ATTENTION: There cannot be a line break because it would be interpreted as a record -->
               <!-- to be inserted where the number of columns would not match that of the table and  -->
               <!-- an exception would be generated, so the first record is attached to the  tag string.  -->
        <string>44018855Z|john|smith
		33873345A|kevin|swatch
		43829209O|peter|petrelli
		27364849E|george|lucas</string>
        </table.load>

        <print.sql>
            <select>
                <columns>*</columns>
                <from table='persones' />
            </select>
        </print.sql>
    </body>
</xsql-script>

The result of the example execution would be as follows:

Copy
+---------+----+---------+
    |dni      |nom |cognom1  |
    +---------+----+---------+
    |44018855Z|john|smith    |
    |33873345A|kevin|swatch  |
    |43829209O|peter|petrelli|
    |27364849E|george|lucas  |
    +---------+----+---------+
Example

Load records contained in a file into a table.

Copy
<xsql-script name='table_load_sample2'>
	<body>
		<table name='persones'>
			<column name='dni' type='char' size='9' />
			<column name='nom' type='varchar' size='20' />
			<column name='cognom' type='varchar' size='25' />
		</table>

		<table.load table='persones'>
			<!-- The file must be indicated as type absolute and indicating the complete
			    path in the attribute name. The content of the file is as follows: 44018855Z|john|smith 
				33873345A|kevin|swatch 43829209O|peter|petrelli 27364849E|george|lucas -->
			<file name="load.unl" type="absolute" />
		</table.load>

		<print.sql>
			<select>
				<columns>*</columns>
				<from table='persones' />
			</select>
		</print.sql>
	</body>
</xsql-script>

The result of the example execution would be as follows:

Copy
+---------+----+---------+
    |dni      |nom |cognom1  |
    +---------+----+---------+
    |44018855Z|john|smith    |
    |33873345A|kevin|swatch  |
    |43829209O|peter|petrelli|
    |27364849E|george|lucas  |
    +---------+----+---------+
Example

Load records from the program itself into a temporary table.

Copy
<xsql-script name='table_load_sample3'>
	<body>
		<table name='persones'>
			<column name='dni' type='char' size='9' />
			<column name='nom' type='varchar' size='20' />
			<column name='cognom' type='varchar' size='25' />
		</table>

		<table.load table='persones'>
			<!-- The file must be indicated as type absolute and indicating the 
			    complete path in the  attribute name. The content of the file is as follows: 
			    44018855Z|john|smith 
				33873345A|kevin|swatch 43829209O|peter|petrelli 27364849E|george|lucas -->
			<file name="load.unl" type="absolute" />
		</table.load>

		<print.sql>
			<select>
				<columns>*</columns>
				<from table='persones' />
			</select>
		</print.sql>
	</body>
</xsql-script>

The result of the example execution would be as follows:

Copy
+---------+----+---------+
    |dni      |nom |cognom1  |
    +---------+----+---------+
    |44018855Z|john|smith    |
    |33873345A|kevin|swatch  |
    |43829209O|peter|petrelli|
    |27364849E|george|lucas  |
    +---------+----+---------+
Example

Load in a table records contained in a file encoded in utf-8 being in an OS in another encoding (for example cp1552 of windows).

Copy
<xsql-script name='table_load_sample4'>
        <body>
            <table name='persones'>
                <column name='dni'     type='char'    size='9'  />
                <column name='nom'     type='varchar' size='20' />
                <column name='cognom'  type='varchar' size='25' />
            </table>

            <table.load table='persones' encoding='utf-8'>
                <!-- The file must be indicated as type absolute  and indicating the complete path in the name attribute. 
                    The content of the file in utf-8 is as follows:
    44018855Z|john|smith
    33873345A|kevin|swatch
    43829209O|peter|petrelli
    27364849E|george|lucas
                -->
                <file name="load.unl" type="absolute" />
            </table.load>

        </body>
    </xsql-script>
Example

Apply formatting with <filter> when loading data from a resultset: capitalize the values of a given column.

Copy
<xsql-script name='table_load_sample5'>
    <body>
        <table name='@tmp_sample' temp='y'>
            <column name='code'         type='char' size='6' />
            <column name='description'  type='varchar' size='60' />
        </table>
                        
        <table.load table='@tmp_sample' replace='insert-first'>
            <instructions>
                <onError>
                  <insert table='log_events'>
                              <column name='log_id'>0</column>
                              <column name='tablename'><p_table/></column>
                              <column name='msglog2'><p_error/></column>
                              <column name='line'><p_line/></column>
                              <column name='row'><p_row/></column>
                              <column name='col'><p_col/></column>
                  </insert>
                </onError>
                <filter>
                    <column name='code'><string.toUpperCase><code /></string.toUpperCase></column>
                </filter>
            </instructions>
            <data>
                <sql.toResultSet>
                    <select>
                        <columns>code1, name</columns>
                        <from table='tabori'/>
                    </select>
                </sql.toResultSet>
            </data>
        </table.load>
    </body>
</xsql-script>

As a result of the execution, the result of the select is obtained and loaded as if it were a physical file.

Example

Apply format with <filter> in the data load: add prefix to a mnemonic code.

Copy
<xsql-script name='table_load_sample6'>
    <body>
        <function name='local_addCodePrefix'>
            <args>
                <arg name='p_code'  type='string' />
            </args>
            <body>
                <return><string><date.year><date.today /></date.year>-<p_code /></string></return>
            </body>
        </function>

        <table name='@tmp_sample' temp='y'>
            <column name='code'     type='char'     size='10' />
            <column name='descri'   type='varchar'  size='60' />
        </table>

        <table.load table='@tmp_sample'>
            <instructions>
                <filter>
                    <column name='code'><local_addCodePrefix><code /></local_addCodePrefix></column>
                </filter>
            </instructions>
            <data>
                <string>
0001|ITEM 0001
0002|ITEM 0002
0003|ITEM 0003
                </string>
            </data>
        </table.load>

        <print.sql head='true'>
            <select>
                <columns>*</columns>
                <from table='@tmp_sample' />
            </select>
        </print.sql>
    </body>
</xsql-script>

The result of the example execution would be as follows:

Copy
+---------+---------+
|code     |descri   |
+---------+---------+
|2012-0001|ITEM 0001|
|2012-0002|ITEM 0002|
|2012-0003|ITEM 0003|
+---------+---------+
Example

Use <onError> for handling load errors without stopping or performing a rollback.

Copy
<xsql-script name='table_load_sample7'>
    <body>

        <function name='createtable'>
            <body>
                <nativesql>
!DROP TABLE test;
CREATE TABLE test(
    a char(10) not null, b CHAR(10) not null, c CHAR(10), d CHAR(10),
PRIMARY KEY (a, b)
)
                </nativesql>
            </body>
        </function>

        <function name='printtable'>
            <body>
                <println>printtable</println>
                <print.sql>
                    <select cache='false'>
                        <columns>*</columns>
                        <from table='test' />
                    </select>
                </print.sql>
            </body>
        </function>

        <function name='callback_error'>
            <args>
                <arg name='_table' />
                <arg name='_row' />
                <arg name='_col' />
                <arg name='_line' />
                <arg name='_error' />
            </args>
            <body>
                <println/>
                <println/>
                <println>Error loading data int table <_table /> at row: <_row/> column: <_col /> line: <_line /> cause: <_error /></println>
                <println/>
                <println/>
            </body>
        </function>
    <!-- SAMPLE: load delimited with interceptor funcions -->

    <println>LOAD INSTRUCTIONS</println>
    <createtable />

    <println>printing table 1</println>
    <printtable />


    <table.load
        table='test'
        columns='a,b,d'
        colindexes='2,1,3'
        delimiter=','
    >
    <instructions>
        <filter>
            <column name='a'>
                <string.toUpperCase><a/></string.toUpperCase>
            </column>
        </filter>
        <onError>
            <callback_error>
                <p_table />
                <p_row />
                <p_col />
                <p_line />
                <p_error />
            </callback_error>
         </onError>
    </instructions>
    <data>
        <string>
c1,b1,a1,d1,e1,f1
c3 this row will generate an exception
c2,b2,a2,dx
c3,b3,a3,d3
        </string>
    </data>
    <!--
    if source is a File we need to wrap it insise tag <data><file .. /></data
    to allow get1EvaluatedArguments pointing to File works
    -->
    </table.load>

    <println>printing table 2</println>
    <printtable />

    </body>
</xsql-script>
Example

Uso de enclosing-char.

Copy
<xsql-script name='table_load_sample7'>
    <body>
	
	        <function name='createtable'>
	            <body>
	                <nativesql>
	!DROP TABLE test;
	CREATE TABLE test(
	    a char(10) not null, b CHAR(10) not null, c CHAR(10), d CHAR(10),
	PRIMARY KEY (a, b)
	)
	                </nativesql>
	            </body>
	        </function>
	
	        <function name='printtable'>
	            <body>
	                <println>printtable</println>
	                <print.sql>
	                    <select cache='false'>
	                        <columns>*</columns>
	                        <from table='test' />
	                    </select>
	                </print.sql>
	            </body>
	        </function>
	
	    <println>LOAD INSTRUCTIONS</println>
	    <createtable />
	
	    <println>printing table 1</println>
	    <printtable />
	
	    <table.load
	        table='test'
	        delimiter=','
	
	    >
	    <data>
	        <string>
	Barcelona,Emp 1 S,A,Del 1, Dep 1
	Manresa,"Emp 2 S,A",Del 2, Dep 2
	        </string>
	    </data>
	    </table.load>
	
	    <println>printing table 2</println>
	<!--
	+=========+=========+=====+======+
	|a        |b        |c    |d     |
	+=========+=========+=====+======+
	|Barcelona|Emp 1 S  |A    |Del 1 |
	|Manresa  |Emp 2 S,A|Del 2|Dep 2 |
	+=========+=========+=====+======+
	-->
	    <printtable />
	
	
	    <println>LOAD INSTRUCTIONS</println>
	    <createtable />
	
	    <println>printing table 1</println>
	    <printtable />
	
	    <table.load
	        table='test'
	        delimiter=';'
	    >
	    <data>
	        <string>
	Barcelona;CUST;CLI;Del 1; Dep 1
	Manresa;"CUST;CLI";Del 2; Dep 2
	        </string>
	    </data>
	    </table.load>
	
	    <println>printing table 2</println>
	<!--
	+=========+========+=====+======+
	|a        |b       |c    |d     |
	+=========+========+=====+======+
	|Barcelona|CUST    |CLI  |Del 1 |
	|Manresa  |CUST;CLI|Del 2|Dep 2 |
	+=========+========+=====+======+
	-->
	    <printtable />
	
	
	    <table.load
	        table='test'
	        delimiter=';'
	        enclosing-char='\$'
	    >
	    <data>
	        <string>
	Barcelona;CUST;CLI;Del 1; Dep 1
	Manresa;$CUST;CLI$;Del 2; Dep 2
	        </string>
	    </data>
	    </table.load>
	
	    <println>printing table 2</println>
	<!--
	+=========+==========+=====+======+
	|a        |b         |c    |d     |
	+=========+==========+=====+======+
	|Barcelona|CUST      |CLI  |Del 1 |
	|Manresa  |$CUST;CLI$|Del 2|Dep 2 |
	+=========+==========+=====+======+
	-->
	    <printtable />
	</body>
</xsql-script>
Example

Load records contained in a file using the attribute colindexes.

Copy
<xsql-script name='table_load_sample2'>
	<body>
		<table name='persones'>
			<column name='dni' type='char' size='9' />
			<column name='nom' type='varchar' size='20' />
			<column name='cognom' type='varchar' size='25' />
		</table>

		<table.load table='persones' columns='dni, nom, cognom'
			colindexes='0,3,5'>
			<!-- The file must be indicated as type absolute and indicating the complete path 
			    in the attribute name. The content of the file is as follows: 44018855Z|jkjk|innecesario|john|blanco|smith 
				33873345A|jkjm|nulo|kevin|paja|swatch 43829209O|dasd|fijo|peter|gris|petrelli 
				27364849E|eqwe|atri|george|negro|lucas -->
			<file name="load.unl" type="absolute" />
		</table.load>

		<print.sql>
			<select>
				<columns>*</columns>
				<from table='persones' />
			</select>
		</print.sql>
	</body>
</xsql-script>

The result of the example execution would be as follows:

Copy
+---------+----+---------+
    |dni      |nom |cognom1  |
    +---------+----+---------+
    |44018855Z|john|smith    |
    |33873345A|kevin|swatch  |
    |43829209O|peter|petrelli|
    |27364849E|george|lucas  |
    +---------+----+---------+
Example

Load records contained in a file using the attribute colwidths.

Copy
<xsql-script name='table_load_sample2'>
	<body>
		<table name='persones'>
			<column name='dni' type='char' size='9' />
			<column name='nom' type='varchar' size='20' />
			<column name='cognom' type='varchar' size='25' />
		</table>

		<table.load table='persones' columns='dni, nom, cognom'
			colindexes='0,3,5' colwitdhs='1,1,10'>
			<!-- The file must be indicated as type absolute and indicating the complete path 
			    in the attribute name. The content of the file is as follows: 44018855Z|jkjk|innecesario|john|blanco|smith 
				33873345A|jkjm|nulo|kevin|paja|swatch 43829209O|dasd|fijo|peter|gris|petrelli 
				27364849E|eqwe|atri|george|negro|lucas -->
			<file name="load.unl" type="absolute" />
		</table.load>

		<print.sql>
			<select>
				<columns>*</columns>
				<from table='persones' />
			</select>
		</print.sql>
	</body>
</xsql-script>

The result of the example execution would be as follows

Copy
+---------+----+---------+
    |dni      |nom |cognom1  |
    +---------+----+---------+
    |4        |j   |smith    |
    |3        |k   |swatch   |
    |4        |p   |petrelli |
    |2        |g   |lucas    |
    +---------+----+---------+
Example

CUpload a file obtained from an FTP stream.

Copy
<xsql-script name='ftp_table_load'>
	<body>
		<nativesql>
			!DROP TABLE test;
			CREATE TABLE test(
			a char(10) not null, b CHAR(10) not null, c CHAR(10)
			)
		</nativesql>
		<ftp host='192.168.10.1' user='xxx' password=yyyy '>
			<ftp.put name='test.dat'>a1,b1,c1

				a2,b2,c2
				a3,b3,c3



			</ftp.put>
			<table.load table='test' delimiter=','>
				<ftp.getPipedInputStream name='test.dat' />
			</table.load>
		</ftp>
	</body>
</xsql-script>