SQL.FetchNext
-------------

Fetches next row from a result set.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [SQL](component_SQL.md) | [2.6](newinversion26.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "SQL.FetchNext"; Command ) 

**MBS**( **"SQL.FetchNext";** /\* Fetches next row from a result set. \*/  
**$Command**) /\* The command reference number gained with [SQL.NewCommand](SQLNewCommand.md). \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| Command | The command reference number gained with [SQL.NewCommand](SQLNewCommand.md). | $Command |

### Result

Returns 0, 1 or error message.

### Description

Fetches next row from a result set.  
Use FetchNext method to fetch row by row from the result set. You need to call it a first time to get to first row after you made a query.  
  
To check whether a result set exists use [SQL.isResultSet](SQLisResultSet.md) method.   
Result is 1 on success or 0 on failure.   
### Examples

Fetch next record:

 MBS ("SQL.FetchNext"; $Command )  
Run command and fetch first record

 # create command   
Set Variable \[ $Command ; Value: MBS ("[SQL.NewCommand](SQLNewCommand.md)"; $Connection ; "SELECT \* FROM Test")\]  
\# run select   
Set Variable \[ $result2 ; Value: MBS ("[SQL.Execute](SQLExecute.md)"; $Command )\]  
\# go to first row   
Set Variable \[ $result3 ; Value: MBS ("SQL.FetchNext"; $Command )\]  
\# $result3 is 1 if we got a record   
\# read first name field from result:   
Set Variable \[ $firstname ; Value: MBS ("[SQL.GetFieldAsText](SQLGetFieldAsText.md)"; $command ; "FirstName")\]  
\# later release command   
Set Variable \[ $r ; Value: MBS ("[SQL.FreeCommand](SQLFreeCommand.md)"; $Command )\]  
Run a stored procedure:

 # new command for calling our test procedure on a Microsoft SQL Server   
Set Variable \[$command ; MBS ( "[SQL.NewCommand](SQLNewCommand.md)"; $Connection ; "dbo.Test" ) \]  
\# set a parameter   
Set Variable \[$r ; MBS ( "[SQL.SetParamAsNumber](SQLSetParamAsNumber.md)"; $Command ; "Param1"; 7 ) \]  
\# execute query   
Set Variable \[$r ; MBS ( "[SQL.Execute](SQLExecute.md)"; $Command ) \]  
\# fetch first record   
Set Variable \[$r ; MBS ( "SQL.FetchNext"; $Command ) \]  
\# get result field   
Set Variable \[$result ; MBS ( "[SQL.GetFieldAsText](SQLGetFieldAsText.md)"; $Command ; 1 ) \]  
### See also

- [SQL.Connect](SQLConnect.md)
- [SQL.Execute](SQLExecute.md)
- [SQL.FetchLast](SQLFetchLast.md)
- [SQL.GetFieldNameList](SQLGetFieldNameList.md)
- [SQL.GetFieldsAsJSON](SQLGetFieldsAsJSON.md)
- [SQL.GetRecordsAsText](SQLGetRecordsAsText.md)
- [SQL.isResultSet](SQLisResultSet.md)
- [SQL.NewCommand](SQLNewCommand.md)
- [SQL.NewConnection](SQLNewConnection.md)
- [SQL.SetParamAsNumber](SQLSetParamAsNumber.md)

### Example Databases

- [SQL to other databases/DuckDB Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/DuckDB%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/Firebird Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/Firebird%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/Microsoft Access Execute](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/Microsoft%20Access%20Execute.shtml#1ScriptAnchor_)
- [SQL to other databases/Microsoft SQL Execute](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/Microsoft%20SQL%20Execute.shtml#1ScriptAnchor_)
- [SQL to other databases/Microsoft SQL Server Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/Microsoft%20SQL%20Server%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/MySQL Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/MySQL%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/ODBC Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/ODBC%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/PostgreSQL Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/PostgreSQL%20Query.shtml#1ScriptAnchor_)
- [SQL to other databases/SQLite blob](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/SQLite%20blob.shtml#3ScriptAnchor_)
- [SQL to other databases/SQLite fun](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/SQLite%20fun.shtml#3ScriptAnchor_)

### Blog Entries

- [Connect to DuckDB in FileMaker](https://www.mbsplugins.de/archive/2025-04-10/Connect_to_DuckDB_in_FileMaker/monkeybreadsoftware_blog_filemaker)
- [Connect to Postgres in FileMaker](https://www.mbsplugins.de/archive/2025-04-09/Connect_to_Postgres_in_FileMak/monkeybreadsoftware_blog_filemaker)
- [Insert and return new record ID](https://www.mbsplugins.de/archive/2024-06-27/Insert_and_return_new_record_I/monkeybreadsoftware_blog_filemaker)
- [Connect to an external database server in FileMaker](https://www.mbsplugins.de/archive/2022-03-11/Connect_to_an_external_databas/monkeybreadsoftware_blog_filemaker)
- [Tip of the day: Connect to MySQL and run a query](https://www.mbsplugins.de/archive/2017-02-07/Tip_of_the_day_Connect_to_MySQ/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 2/2022, Seite 28](https://filemaker-magazin.de/neuigkeit/4174-Appetithappen-FMM_202202)
- [Ausgabe 4/2016, Seite 28](https://filemaker-magazin.de/neuigkeit/3876-Appetithappen-FMM_201604)
- [Ausgabe 3/2016, Seite 30](https://filemaker-magazin.de/neuigkeit/3883-Appetithappen-FMM_201603)
- [Ausgabe 2/2016, Seite 14](https://filemaker-magazin.de/neuigkeit/3879-Appetithappen-FMM_201602)
- [Ausgabe 1/2016, Seite 29](https://filemaker-magazin.de/neuigkeit/3870-Appetithappen-FMM_201601)

This function is free to use.

Created 18th August 2014 , last changed 25th February 2026

  
[SQL.FetchLast](SQLFetchLast.md) - [SQL.FetchPos](SQLFetchPos.md)

[HTML Version](SQLFetchNext.shtml)