Query

Execute a SQL query to the default or specified datasource.

Component Signature

<bx:Query name=[string]
datasource=[string]
returnType=[string]
columnKey=[string]
dbtype=[string]
maxRows=[integer]
blockfactor=[integer]
fetchsize=[integer]
timeout=[integer]
cache=[boolean]
cacheTimeout=[duration]
cacheLastAccessTimeout=[duration]
cacheKey=[string]
cacheProvider=[string]
result=[string]
clientInfo=[struct] />

Attributes

Atrribute
Type
Required
Description
Default

name

string

false

The name of the variable to store the query results in.

datasource

string

false

The datasource to execute the query against.

returnType

string

false

The type of the result to return. One of: query, struct, array.

query

columnKey

string

false

The key to use for the column names in the result struct.

dbtype

string

false

The type of query to execute. One of: query, hql.

maxRows

integer

false

The maximum number of rows to return. -1 for no limit.

-1

blockfactor

integer

false

Maximum rows per block to fetch from the server. Ranges from 1-100.

[ortus.boxlang.runtime.validation.dynamic.Min@14144cc9, ortus.boxlang.runtime.validation.dynamic.Max@259647f2]

fetchsize

integer

false

The number of rows to fetch at a time. Ranges from 1-100.

timeout

integer

false

The timeout for the query in seconds.

cache

boolean

false

Whether or not to cache the results of the query.

false

cacheTimeout

duration

false

The timeout for the cached query, using a duration object like createTimespan( 0, 1, 0, 0 ).

cacheLastAccessTimeout

duration

false

The timeout for the cached query, using a duration object like createTimespan( 0, 1, 0, 0 ).

cacheKey

string

false

The key to use for the cached query.

cacheProvider

string

false

String name of the cache provider to use. Defaults to the default cache provider.

result

string

false

The name of the variable to store the query result in.

clientInfo

struct

false

Examples

Simple Query Usage

A simple query needs nothing more than a SQL statement:

Though, for SELECT queries, a variable name is necessary to acquire the results object:

By default, the results will be in Query format. Array and Struct results are also supported:

For struct results, use columnKey to define the column name which will form the struct key in the resulting struct object:

Caching Query Results

Queries can be cached by specifying cache=true and a cache timespan:

You can customize the cache usage by creating a custom cache configuration in your boxlang.json:

Then reference the cache name in the query cacheProvider attribute:

Empty query

Create an empty query object

Query with some data

Create query object with some initial data

Additional Examples

Last updated

Was this helpful?