Run a query against a Data Source Object.

Query runs a query against a Data Source, returning a resultset into a named array. The array is keyed on the returned column (or attribute) name. The body of the query is executed once for each returned row.

query requires named parameters. The parameters required will differ depending on whether the Data Source is of type ODBC or LDAP.

The parameters are:

Parameter Description
datasource Mandatory: The name of the Data Source to query.
result Mandatory: The name of the array in which each result row should be written.
attributes Mandatory for LDAP Data Sources only: An array of attributes to retrieve from the LDAP record.
filter Mandatory for LDAP Data Sources only: A string indicating the LDAP Search Filter in RFC 2254 format.
searchbase Optional. LDAP Data Sources only: The starting point of the search in the LDAP hierarchy.
sql Optional. ODBC Data Sources only: The SQL to execute against the specified Data Source. If the SQL is a query (SELECT) then the resulting rows are returned into the array specified by the result parameter.


In the following examples, assume there is a table called “customers” defined as follows:


id int,

first\_name char(30),

last\_name char(30));

Example: Execute a query against an ODBC connection and print the results:

// Print out each matching record from the database

query(datasource: "myodbc",

sql: "select \* from customers",

result: arr) {

echo "customer id: ${arr.id}";

echo "first name: ${arr.first\_name}";

echo "last name: ${arr.last\_name}";

echo "\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*";

Last modified December 18, 2020: reorganzise sections (85d5aef)