> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/fetch.md).

# FETCH

## Syntax

```bnf
FETCH cursor_name INTO var_name [, var_name] ...
```

## Description

This statement fetches the next row (if a row exists) using the specified [open](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/open.md) [cursor](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors.md), and advances the cursor pointer.

`var_name` can be a [local variable](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-variable.md), but *not* a [user-defined variable](/docs/server/reference/sql-structure/sql-language-structure/user-defined-variables.md).

If no more rows are available, a No Data condition occurs with`SQLSTATE` value `02000`. To detect this condition, you can set up a handler for it (or for a `NOT FOUND` condition).

See [Cursor Overview](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/cursor-overview.md) for an example.

## See Also

* [Cursor Overview](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/cursor-overview.md)
* [DECLARE CURSOR](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/declare-cursor.md)
* [OPEN cursor\_name](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/open.md)
* [CLOSE cursor\_name](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/close.md)
* [Cursors in Oracle mode](/docs/release-notes/community-server/about/compatibility-and-differences/sql_modeoracle.md)

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors/fetch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
