The "getRecordsUsingSQL" action runs a SQL query and returns the resulting records. It is particularly useful when joining data from multiple tables, for aggregating data, for running ad hoc queries, and for sorting results by unindexed fields.
It is the most efficient way to return all records from a SQL query. When query results contain too many records to retrieve all at once, use a cursor, which makes it easy, fast, and efficient to paginate results. See Most effective ways to return data from the JSON DB API. Returning a cursor and returning records directly are mutually exclusive operations.
Only the SQL SELECT statement can be used. Any other SQL statements fail and return an error. The only unusual requirement is that JSON requires the double-quote character to be escaped by the backslash character. The semicolon that SQL requires at the end of a SELECT statement is optional.
Request examples
Minimal
{
"action": "getRecordsUsingSQL",
"params": {
"sql": "SELECT * from athlete"
},
"authToken": "replaceWithAuthTokenFromCreateSession"
}Cursor
{
"api": "db",
"action": "getRecordsUsingSQL",
"params": {
"sql": "SELECT * from athlete",
"returnCursor": true
},
"requestId": "2",
"authToken": "replaceWithAuthTokenFromCreateSession"
}Maximal
{
"api": "db",
"apiVersion": "1.0",
"requestId": "3",
"action": "getRecordsUsingSQL",
"params": {
"databaseName": "ctreeSQL",
"sql": "SELECT * from athlete WHERE earnings > ?",
"sqlParams": [
1000000000
],
"returnCursor": false,
"forceRecordCount": true,
"sqlForwardOnly": true
},
"responseOptions": {
"binaryFormat": "hex",
"dataFormat": "objects",
"numberFormat": "string",
"variantFormat": "variantObject",
"includeFields": [],
"excludeFields": []
},
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
}
Response examples
Minimal
{
"result": {
"dataFormat": "objects",
"numberFormat": "number",
"binaryFormat": "hex",
"fields": [
{
"name": "id",
"type": "bigint"
},
{
"name": "changeid",
"type": "bigint"
},
{
"name": "name",
"type": "varchar",
"length": 30
},
{
"name": "ranking",
"type": "smallint"
},
{
"name": "birthdate",
"type": "date"
},
{
"name": "playernumber",
"type": "numeric",
"length": 32,
"scale": 6
},
{
"name": "livedpast2000",
"type": "bit"
},
{
"name": "earnings",
"type": "money",
"length": 32,
"scale": 4
},
{
"name": "favoritesaying",
"type": "varchar",
"length": 500
}
],
"data": [
{
"birthdate": "1963-02-17",
"changeid": 1291366,
"earnings": 1700000000,
"favoritesaying": "There is no 'i' in team but there is in win.",
"id": 1,
"livedpast2000": true,
"name": "Michael Jordan",
"playernumber": 23,
"ranking": 1
},
{
"birthdate": "1942-01-17",
"changeid": 1291366,
"earnings": 60000000,
"favoritesaying": "Float like a butterfly, sting like a bee.",
"id": 3,
"livedpast2000": true,
"name": "Muhammad Ali",
"playernumber": 1,
"ranking": 3
},
{
"birthdate": "1895-02-06",
"changeid": 1291366,
"earnings": 800000,
"favoritesaying": "Every strike brings me closer to the next home run.",
"id": 2,
"livedpast2000": false,
"name": "Babe Ruth",
"playernumber": 3,
"ranking": 2
},
{
"birthdate": "1940-10-23",
"changeid": 1291366,
"earnings": 115000000,
"favoritesaying": "Everything is practice.",
"id": 4,
"livedpast2000": true,
"name": "Pele",
"playernumber": 10,
"ranking": 4
},
{
"birthdate": "1961-01-26",
"changeid": 1291366,
"earnings": 1720000,
"favoritesaying": "You miss 100 percent of the shots you never take.",
"id": 5,
"livedpast2000": true,
"name": "Wayne Gretzky",
"playernumber": 99,
"ranking": 5
},
{
"birthdate": "1969-01-03",
"changeid": 1291366,
"earnings": 990000000,
"favoritesaying": "Once something is a passion, the motivation is there.",
"id": 6,
"livedpast2000": true,
"name": "Michael Schumacher",
"playernumber": 1,
"ranking": 6
}
],
"moreRecords": false,
"requestedRecordCount": 6,
"returnedRecordCount": 6,
"totalRecordCount": 6
},
"debugInfo": {
"request": {
"action": "getRecordsUsingSQL",
"params": {
"sql": "SELECT * from athlete"
},
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
},
"serverSuppliedValues": {
"databaseName": "ctreeSQL",
"ownerName": null
},
"errorData": {
"errorData": null
},
"warnings": []
},
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}Cursor
{
"result": {
"cursorId": "cursorId",
"totalRecordCount": 6
},
"requestId": "2",
"debugInfo": {
"request": {
"api": "db",
"action": "getRecordsUsingSQL",
"params": {
"sql": "SELECT * from athlete",
"returnCursor": true
},
"requestId": "2",
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
},
"serverSuppliedValues": {
"databaseName": "ctreeSQL",
"ownerName": null
},
"errorData": {
"errorData": null
},
"warnings": []
},
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}Maximal
{
"result": {
"dataFormat": "objects",
"numberFormat": "string",
"binaryFormat": "hex",
"fields": [
{
"name": "id",
"type": "bigint"
},
{
"name": "changeid",
"type": "bigint"
},
{
"name": "name",
"type": "varchar",
"length": 30
},
{
"name": "ranking",
"type": "smallint"
},
{
"name": "birthdate",
"type": "date"
},
{
"name": "playernumber",
"type": "numeric",
"length": 32,
"scale": 6
},
{
"name": "livedpast2000",
"type": "bit"
},
{
"name": "earnings",
"type": "money",
"length": 32,
"scale": 4
},
{
"name": "favoritesaying",
"type": "varchar",
"length": 500
}
],
"data": [
{
"birthdate": "1963-02-17",
"changeid": "1291366",
"earnings": "1700000000",
"favoritesaying": "There is no 'i' in team but there is in win.",
"id": "1",
"livedpast2000": true,
"name": "Michael Jordan",
"playernumber": "23",
"ranking": "1"
}
],
"moreRecords": false,
"requestedRecordCount": 1,
"returnedRecordCount": 1,
"totalRecordCount": 1
},
"requestId": "3",
"debugInfo": {
"request": {
"api": "db",
"action": "getRecordsUsingSQL",
"params": {
"databaseName": "ctreeSQL",
"sql": "SELECT * from athlete WHERE earnings > ?",
"sqlParams": [
1000000000
],
"returnCursor": false,
"forceRecordCount": true,
"sqlForwardOnly": true
},
"apiVersion": "1.0",
"requestId": "3",
"responseOptions": {
"binaryFormat": "hex",
"dataFormat": "objects",
"numberFormat": "string",
"variantFormat": "variantObject",
"includeFields": [],
"excludeFields": []
},
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
},
"serverSuppliedValues": {
"databaseName": "ctreeSQL",
"ownerName": null
},
"errorData": {
"errorData": null
},
"warnings": []
},
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}
Properties
Request properties ("params")
| Property | Description | Default | Type | Limits (inclusive) |
|---|---|---|---|---|
databaseName |
The You specify this property when you want to use a different database instead of the default. This property is useful because objects, such as tables and code packages, can have the same name in multiple databases. This feature allows you to create multiple environments in the same server and reuse the same JSON actions in each environment. For example, you can create It is an error to set If no default database is specified during |
Defaults to the session's "defaultDatabaseName" property |
string | 1 to 64 bytes |
forceRecordCount |
The "forceRecordCount" property forces a query to return a record count when true.
|
Optional with default of |
Boolean |
|
returnCursor |
The
|
Optional with default of false
|
Boolean |
|
sql |
The |
Required - No default value |
string | 1 to 32,000,000 bytes |
sqlForwardOnly |
The |
Optional with default of |
Boolean |
|
sqlParams |
The
|
Optional with default of []
|
array | An array of values |
Response properties ("result")
| Property | Description | Type | Limits (inclusive) |
|---|---|---|---|
binaryFormat |
The "binaryFormat" property designates the format of binary values embedded in JSON strings. See binaryFormat for more details. |
string | One of the following: "base64", "hex", or "byteArray". |
data |
The |
array of objects | The action determines its contents. |
dataFormat |
The
|
string |
|
fields |
The
"fields": [
{
"autoValue": "none",
"name": "name",
"type": "varchar",
"length": 50,
"scale": null,
"defaultValue": null,
"nullable": false
}
] |
array |
|
moreRecords |
The "moreRecords" property indicates if there are more records that match the filters in the request. |
Boolean |
|
numberFormat |
The When For example, including Possible values:
|
string |
|
requestedRecordCount |
The
|
integer |
|
returnedRecordCount |
The
|
integer |
|
totalRecordCount |
The
|
integer |
|
Essential information
Notice JSON DB API transactions do not support SQL stored procedures, stored functions, and triggers. It also does not support
"revertTransactionToSavepoint"when the transaction contains a call to"getRecordsUsingSQL"or"runSqlStatements". Unsupported actions return an error.
- This and all FairCom JSON actions that run SQL statements use the FairCom "Direct SQL" API and as such access the data at full speed.
- The
"sqlParams"property is an optional array of objects (containing zero or more SQL parameters) embedded in the SQL statement. It specifies the value of each parameter. SQL parameters are used instead of embedded values in the SQL statements as it makes it easier to reuse SQL code. It also prevents SQL injection attacks. - Each object in the array represents one SQL parameter.
- Each object contains the parameter’s name and its value.
- For SQL and
"sqlParams"objects, put a semicolon in front of the parameter name. - JSON DB API does not support specifying parameters by ordinal position because changing the parameter position in the SQL statement introduces bugs and makes the code harder to read.
- If the fields returned by the SQL statement include JSON, you optionally can use the
"includePaths"property to filter which JSON properties are returned. - If the results can be processed with one call to the operation, use
"maxRecords"with a value of-1to return all the results. - If the results are large, set
"returnCursor"totrueand use"getRecordsFromCursor"to process the results.
- Results return less quickly when:
- The SQL query processes many records.
- There are many records in the results.
- Do not call this action repeatedly to return paginated results; it reruns the SQL query. Instead, you must return a cursor.
- By default, the
"totalRecordCount"property in the response message is-1. To get an exact count, set the"forceRecordCount"totruein the request message. It will count all the records returned by the query. Forcing a record count may be a resource-intensive and time consuming process when the query walks many filtered records.