# Execute SQL

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

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.

```text
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

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>query</code><span class="mo-api-field__type">string</span></dt><dd>the SQL to execute.</dd></div>
  <div class="mo-api-field"><dt><code>db_name</code><span class="mo-api-field__type">string</span></dt><dd>The database used when executing SQL.</dd></div>
  <div class="mo-api-field"><dt><code>sql_type</code><span class="mo-api-field__type">string</span></dt><dd>Caller-defined SQL type identifier.</dd></div>
  <div class="mo-api-field"><dt><code>offset</code><span class="mo-api-field__type">integer</span></dt><dd>The starting position of the first result window.</dd></div>
  <div class="mo-api-field"><dt><code>limit</code><span class="mo-api-field__type">integer</span></dt><dd>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.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Request example</p>
```

```bash
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](get-execution-status.md) to get the status and Statement ID.


The response fields are as follows.

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">integer</span></dt><dd>200 on success.</dd></div>
  <div class="mo-api-field"><dt><code>data.id</code><span class="mo-api-field__type">string</span></dt><dd>Query ID, same as data.query_id.</dd></div>
  <div class="mo-api-field"><dt><code>data.query_id</code><span class="mo-api-field__type">string</span></dt><dd>Query ID; used to query status or cancel execution.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Successful response example</p>
```

```json
{
  "code": 200,
  "data": {
    "id": "query_01",
    "query_id": "query_01"
  }
}
```

:::
::::

## Error response

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>Error code.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>Readable error message.</dd></div>
  <div class="mo-api-field"><dt><code>data</code><span class="mo-api-field__type">null</span></dt><dd>`null` in an error response.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Error response example</p>
```

```json
{"code": 400, "message": "请求参数无效"}
```

:::
::::

## Follow-up operations

Logging `data.query_id` (same as `data.id`). First [query execution status](get-execution-status.md); after the status indicates success, then [query execution result](get-execution-result.md) or [download execution result](download-execution-result.md). [Cancel execution](cancel-execution.md) when aborted.
