Execute SQL¶
Execute a SQL and return the query ID of this execution. Use this ID to query the running status; after the query is successful, the status response returns the result ID (statement_id) required to read or download the results.
POST https://moi.matrixorigin.cn/newmoi/query/execute
Preparation before calling¶
Prepare a personal access token and workspace ID that has access to the target workspace. The identity executing the SQL also needs to have appropriate permissions on the target database; do not splice unprocessed user input directly into the SQL.
Request body¶
querystring- the SQL to execute.
db_namestring- The database used when executing SQL.
sql_typestring- Caller-defined SQL type identifier.
offsetinteger- The starting position of the first result window.
limitinteger- The number of rows in the result window; when 0 is not provided or is less than or equal to 0, the server uses 1000.
Request example
curl -X POST "https://moi.matrixorigin.cn/newmoi/query/execute" \
-H "X-API-Key: $AI_STUDIO_API_KEY" \
-H "X-Workspace-ID: $WORKSPACE_ID" \
-H 'Content-Type: application/json' \
-d '{
"db_name": "sales",
"query": "SELECT 1 AS n",
"offset": 0,
"limit": 20
}'
Successful response¶
The response returns only the query ID. Use the ID query execution status to get the status and Statement ID.
The response fields are as follows.
codeinteger- 200 on success.
data.idstring- Query ID, same as data.query_id.
data.query_idstring- Query ID; used to query status or cancel execution.
Successful response example
{
"code": 200,
"data": {
"id": "query_01",
"query_id": "query_01"
}
}
Error response¶
codestring- Error code.
msgstring- Readable error message.
datanull- `null` in an error response.
Error response example
{"code": 400, "message": "请求参数无效"}
Follow-up operations¶
Logging data.query_id (same as data.id). First query execution status; after the status indicates success, then query execution result or download execution result. Cancel execution when aborted.