FM.SQL.CSV
----------

Queries text of SQL result for CSV export.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [FM](component_FM.md) [FMSQL](component_FMSQL.md) | [7.0](newinversion70.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "FM.SQL.CSV"; SQLref { ; FirstRow; LastRow; firstCol; lastCol; rowSeparator; colSeparator; Flags } ) 

**MBS**( **"FM.SQL.CSV";** /\* Queries text of SQL result for CSV export. \*/  
**$SQLref**; /\* The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. \*/   
**$FirstRow**; /\* Optional; The index of first row.  
Default is 0.e.g. 0 \*/   
**$LastRow**; /\* Optional; The index of last row.  
Default is [FM.SQL.RowCount](FMSQLRowCount.md)-1 if no value is given.e.g. 5 \*/   
**$firstCol**; /\* Optional; The index of first column.  
Default is 0.e.g. 0 \*/   
**$lastCol**; /\* Optional; The index of last column.  
Default is [FM.SQL.FieldCount](FMSQLFieldCount.md)-1 if no value is given.e.g. 6 \*/   
**$rowSeparator**; /\* Optional; The row separator.  
Default is CR.  
Can be more than one character.e.g. Char(13) \*/   
**$colSeparator**; /\* Optional; The column separator.  
Default is semicolon.  
Can be more than one character.e.g. ";" \*/   
**$Flags**) /\* Optional; Various flags.  
1: Pass 1 to put all text values in quotes.e.g. 0 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SQLref | The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. | $SQLRef |  |
| FirstRow | The index of first row.   Default is 0. | 0 | Optional |
| LastRow | The index of last row.   Default is [FM.SQL.RowCount](FMSQLRowCount.md)-1 if no value is given. | 5 | Optional |
| firstCol | The index of first column.   Default is 0. | 0 | Optional |
| lastCol | The index of last column.   Default is [FM.SQL.FieldCount](FMSQLFieldCount.md)-1 if no value is given. | 6 | Optional |
| rowSeparator | The row separator.   Default is CR.   Can be more than one character. | Char(13) | Optional |
| colSeparator | The column separator.   Default is semicolon.   Can be more than one character. | ";" | Optional |
| Flags | Various flags.   1: Pass 1 to put all text values in quotes. | 0 | Optional      Added in version **13.0**. |

### Result

Returns text or error.

### Description

Queries text of SQL result for CSV export.  
You can use this method to quickly get all fields in a given area as text.  
If needed, we put values in quotes and escape quotes and newlines.  
  
For numeric values we ask FileMaker to provide text representation, so FileMaker may use dot or comma as decimal divider depending on your locale.  
  
Please use [Text.WriteTextFile](TextWriteTextFile.md) to write to text file if you need CSV export.  
### Examples

Query fields and export as text and CSV:

 Set Variable \[$sql ; Value:MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; ""; "select \* from Clients")\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["SQL error"; $sql \]  
Else  
 Show Custom Dialog \["SQL result"; "got " &amp; MBS ( "[FM.SQL.RowCount](FMSQLRowCount.md)"; $sql ) &amp;" rows with each " &amp; MBS ( "[FM.SQL.FieldCount](FMSQLFieldCount.md)"; $sql ) &amp; " …"\]  
 # Write to text file  
 Set Variable \[$text ; Value:MBS ( "[FM.SQL.Text](FMSQLText.md)"; $sql )\]  
 Set Variable \[$desktopFolder ; Value:MBS ( "[Folders.UserDesktop](FoldersUserDesktop.md)" )\]  
 Set Variable \[$path ; Value:MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $desktopFolder ; "fields.txt" )\]  
 Set Variable \[$r ; Value:MBS ( "[Text.WriteTextFile](TextWriteTextFile.md)"; $text ; $path ; "UTF8")\]  
 # Write to text file  
 Set Variable \[$text ; Value:MBS ( "FM.SQL.CSV"; $sql )\]  
 Set Variable \[$path ; Value:MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $desktopFolder ; "fields.csv" )\]  
 Set Variable \[$r ; Value:MBS ( "[Text.WriteTextFile](TextWriteTextFile.md)"; $text ; $path ; "UTF8")\]  
 # Cleanup  
 Set Variable \[$r ; Value:MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $sql )\]  
End If  
### See also

- [FM.SQL.Execute](FMSQLExecute.md)
- [FM.SQL.Field](FMSQLField.md)
- [FM.SQL.FieldCount](FMSQLFieldCount.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [FM.SQL.RowCount](FMSQLRowCount.md)
- [FM.SQL.Text](FMSQLText.md)
- [Folders.UserDesktop](FoldersUserDesktop.md)
- [Path.AddPathComponent](PathAddPathComponent.md)
- [SQL.FieldCount](SQLFieldCount.md)
- [Text.WriteTextFile](TextWriteTextFile.md)

### Release notes

- **Version 13.0**
    - Added new flags parameter to [FM.SQL.CSV](https://www.mbsplugins.eu/FMSQLCSV.shtml) and [Matrix.CSV](https://www.mbsplugins.eu/MatrixCSV.shtml).
    - Changed [FM.SQL.Text](https://www.mbsplugins.eu/FMSQLText.shtml) and [FM.SQL.CSV](https://www.mbsplugins.eu/FMSQLCSV.shtml) functions to return empty text if there are no rows and no row parameter.
- **Version 8.2**
    - Improved [FM.SQL.CSV](http://www.mbsplugins.eu/FMSQLCSV.shtml) function to no longer quote numbers.
- **Version 7.0**
    - Added [FM.SQL.Text](http://www.mbsplugins.eu/FMSQLText.shtml) and [FM.SQL.CSV](http://www.mbsplugins.eu/FMSQLCSV.shtml).

### Blog Entries

- [MBS FileMaker Advent calendar - Door 19 - CSV and TSV](https://www.mbsplugins.de/archive/2025-12-19/MBS_FileMaker_Advent_calendar_/monkeybreadsoftware_blog_filemaker)
- [New in MBS FileMaker Plugin 13.0](https://www.mbsplugins.de/archive/2023-01-24/New_in_MBS_FileMaker_Plugin_13/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.0pr5](https://www.mbsplugins.de/archive/2023-01-02/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.6pr2](https://www.mbsplugins.de/archive/2022-12-07/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [FileMaker records to XML or JSON](https://www.mbsplugins.de/archive/2020-06-30/FileMaker_records_to_XML_or_JS/monkeybreadsoftware_blog_filemaker)
- [Query FileMaker records as JSON](https://www.mbsplugins.de/archive/2020-04-03/Query_FileMaker_records_as_JSO/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 8.2pr3](https://www.mbsplugins.de/archive/2018-04-18/MBS_FileMaker_Plugin_version_8/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 7.0pr2](https://www.mbsplugins.de/archive/2017-01-09/MBS_FileMaker_Plugin_version_7/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 3/2018, Seite 34](https://filemaker-magazin.de/neuigkeit/3953-Appetithappen-FMM_201803)

This function checks for a license.

Created 8th January 2017 , last changed 31st March 2023

  
[FM.SQL.Avg](FMSQLAvg.md) - [FM.SQL.Execute](FMSQLExecute.md)

[HTML Version](FMSQLCSV.shtml)