Package to obtain data from the database.

1 select

Use the SELECT statement to retrieve values from a database or from an SPL. Rows or values that satisfy the specified search criteria of the query are called qualifying rows or values. What the query retrieves to its calling context, after applying any additional logical conditions, is called the result set of the query. This result set can be empty.

<select
    prefix='prefix'
    struct='struct'
    required='true|false'
    intotemp='intotemp'
    type='type'
    cache='true|false'
    prepare='true|false'
    prepare-cache='true|false'
    secure='true|false'
    oracle='ansi'
    iwa-accelerat='on|off'
    iwa-fallback='on|off'
    config='true|false'
>
    <columns> !
        <cols /> +
    </columns>
    <from table='table'> !
        <join
            table='table'
            type='left'
            alias='alias'
        > ?
            <on> +
                <join_tables_condition /> +
            </on>
        </join>
    </from>
    <where> !
        <where /> !
    </where>
    <group> !
        <groupby /> !
    </group>
    <having> !
        <having /> !
    </having>
    <order> !
        <orderby /> !
    </order>
</select>

Exceptions

database required

You need to specify the database.

The specified table is not in the database.

The table does not exists.

not found [required attribute]

A required attribute has not been informed.

a subquery has returned more than one row

The subquery returns more than one register.

intotemp select, can not return data

Remarks

To execute a query is necessary that the user will be identified and that a database will be selected. This is informed through the options -user and -dbms of the command ws-dbscript.

The querys in XSQL-Script offers several advantages. For this cases in whose that the result of the SELECT sentence is composed of a single register and in which each value of the tupla is required to be asigned to a variable, you can use the attribute prefix to get automatically the following:

Creation of as many variables as there are fields on the label <columns>. The name of each variable is formed by the concatenation of the prefix (value of the attribute prefix) with the name of the column. For those cases in which the resulting column will be an adittion function (COUNT, SUM...), you should specify an alias. The type of each variable is adopted in function of the data type of each value resulting of the query. Each variable is assigned the corresponding value of the recordset.

The default value of the attribute cache is false. Through this attribute is possible to store in memory a query for a fix variable values. In this way, when a query is executed again, in which those variables contains the same value than the cached query, the query is reused, obtaining the immediate result (the same which was obtained in the stored query). When you perform a query in XSQL-Script, it is converted to the SQL syntax native og the database agent (option -dbms)

XSQL-Script SQL native (converted)

Remarks

If then the user execute a query whose content of variables was identical to the variables of the image query, he will obtain the result immediately because this query is stored in memory (cache).

The default value of the attribute prepare is true. Through this attribute is possible to prepare a query in which the variables are replaced by '?'. If in addition it has not been indicated prepare-cache='false', the prepare is stored in cache, in this way, when a query is executed again with the same structure than the 'prepared' query, the question marks are replaced by the corresponding value in the variables of the current query, getting a streamlining of the selection process.

Start of prepare Prepare 1
Prepare 2 Prepare 3

Remarks

It exists an exception to 'prepare' the query. If this query has the tag <nvl> and also has informed the attribute prepare (by default true), an execution error is produced.

XSQL-Script with <nvl>
Start of Prepare with error

Remarks

To avoid this exception is necessary to inform the attribute prepare-cache with value false.

Example

Select registers of a table through search condition.

Copy
<xsql-script name='select_sample1'>
    <body>
        <print.sql>
            <select>
                <columns>linid, cabid, codean, codart, varstk</columns>
                <from table='garticul' />
                <where>linid = '0'</where>
            </select>
        </print.sql>
    </body>
</xsql-script>

The content of the columns linid, cabid, codean, codart and varstk of the table garticul are shown on screen for all the registers which accomplish linid = 0.

Example

Select registers of a table through search condition and group.

Copy
<xsql-script name='select_sample2'>
   <body>
       <print.sql>
           <select>
               <columns>linid, cabid, codean, codart, varstk</columns>
               <from table='garticul' />
               <where>linid = '0'</where>
               <group>cabid</group>
           </select>
       </print.sql>
   </body>
</xsql-script>

The content of the columns linid, cabid, codean, codart and varstk of the table garticul are shown on screen for all the registers which accomplish linid = 0. The registers are grouped by the column cabid.

Example

Select registers of a table through search condition, group and group condition.

Copy
<xsql-script name='select_sample3'>
   <body>
       <print.sql>
           <select>
               <columns>linid, cabid, codean, codart, varstk</columns>
               <from table='garticul' />
               <where>linid = '0'</where>
               <group>cabid</group>
               <having>count(cabid) > 3</having>
           </select>
       </print.sql>
   </body>
</xsql-script>

The content of the columns linid, cabid, codean, codart and varstk of the table garticul are shown on screen for all the registers which contains linid = 0. The registers which accomplish the group condition (having) are shown grouped by the column cabid.

Example

Select registers of a table through search condition, group, group condition and order.

Copy
<xsql-script name='select_sample4'>
   <body>
       <print.sql>
           <select>
               <columns>
                   linid, cabid, codean, codart,
                   varstk
               </columns>
               <from table='garticul' />
               <where>linid = '0'</where>
               <group>cabid</group>
               <having>count(cabid) > 3</having>
               <order>2</order>
           </select>
       </print.sql>
   </body>
</xsql-script>

The content of the columns linid, cabid, codean, codart and varstk of the table garticul are shown on screen for all the registers which contains linid = 0. The registers which accomplish the group condition are shown grouped by the column cabid. The registers appears ordered by the field cabid.

Example

Select registers of a table and put it in a variable of struct type.

Copy
<xsql-script name='select_sample5'>
   <body>
       <foreach>
           <select struct='s_garticul>
               <columns>
                   code, name
               </columns>
               <from table='garticul' />
               <where>codart = 'A1'</where>
           </select>
           <do>
               <println><string>Codigo. <s_garticul.code/> Name: <s_garticul.name/></string></println>
           </do>
       </foreach>
   </body>
</xsql-script>

Create a selection cursor which deposit the selected data in a variables of struct type created automatically through the types and column names of the selected data.

Info

When a SQL query is estimated that it will be executed many times, invariably and during differents script executions, it is recommended to indicate to the interpret to store it in memory (cache). In this way, you get that the script will be more efficient in execution time.

Info

The attribute columns and the argument <columns> are excluding.