# 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.

```text
POST $CLOUDSIGMA_API_BASE/query/execute
```

## Before you call this API

Configure the [regional API endpoint and authentication](https://omnifabric.cloudsigma.com/docs/developer/endpoints-and-authentication.html.md), and select 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

<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

<p class="mo-api-example-label">Request example</p>
```bash
curl -X POST "$CLOUDSIGMA_API_BASE/query/execute" \
  -H "X-API-Key: $CLOUDSIGMA_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](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/sql-execution/get-execution-status.html.md) to get the status and Statement ID.


The response fields are as follows.

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

<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

<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

<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

<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](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/sql-execution/get-execution-status.html.md); after the status indicates success, then [query execution result](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/sql-execution/get-execution-result.html.md) or [download execution result](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/sql-execution/download-execution-result.html.md). [Cancel execution](https://omnifabric.cloudsigma.com/docs/reference/api/ai-studio/data-processing/sql-execution/cancel-execution.html.md) when aborted.
