FM.SQL.XMLRecords
-----------------

Returns rows as XML.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [FM](component_FM.md) [FMSQL](component_FMSQL.md) [XML](component_XML.md) | [10.3](newinversion103.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "FM.SQL.XMLRecords"; SQLref; RootNodeName; RecordNodeName; FieldNames { ; Flags; FirstRow; LastRow } ) 

**MBS**( **"FM.SQL.XMLRecords";** /\* Returns rows as XML. \*/  
**$SQLref**; /\* The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. \*/   
**$RootNodeName**; /\* The name for the root XML node for all records.  
Defaults to "records".e.g. "Companys" \*/   
**$RecordNodeName**; /\* The name for the parent XML nodes for ome record.  
Defaults to "record".e.g. "Company" \*/   
**$FieldNames**; /\* A list of field names for the XML.e.g. "Model¶Names" \*/   
**$Flags**; /\* Optional; The flags for the XML creation.  
Pass 1 to get all values as text.  
Pass 2 to get all dates, times and timestamps in SQL format.e.g. 1+2 \*/   
**$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.e.g. 5 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SQLref | The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. | $SQLRef |  |
| RootNodeName | The name for the root XML node for all records.   Defaults to "records". | "Companys" |  |
| RecordNodeName | The name for the parent XML nodes for ome record.   Defaults to "record". | "Company" |  |
| FieldNames | A list of field names for the XML. | "Model¶Names" |  |
| Flags | The flags for the XML creation.   Pass 1 to get all values as text.   Pass 2 to get all dates, times and timestamps in SQL format. | 1+2 | Optional |
| FirstRow | The index of first row.   Default is 0. | 0 | Optional      Added in version **14.2**. |
| LastRow | The index of last row.   Default is [FM.SQL.RowCount](FMSQLRowCount.md)-1. | 5 | Optional      Added in version **14.2**. |

### Result

Returns OK or error.

### Description

Returns rows as XML.  
You provide list of field names, which should be in same order as fields in SQL result.  
Optionally we can return SQL dates, times and timestamps as SQL format.  
Containers are returned as Base64 encoded data.  
### Examples

Query records as XML:

 # run query to get some data   
Set Variable \[ $sql ; Value: MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; ""; "SELECT \\"First\\", \\"Last\\", Company, \\"Photo Container\\" FROM Contacts") \]   
\# query data   
Set Variable \[ $xml ; Value: MBS ( "FM.SQL.XMLRecords"; $sql ; "people"; "person"; "First¶Last¶Company¶Photo"; 2) \]   
\# store result   
Set Field \[ Contacts::Output ; $xml \]   
\# free memory   
Set Variable \[ $xml ; Value: MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $sql ) \]  
Query contacts with related phone records as XML:

 # Run query to fetch some records   
Set Variable \[ $sql1 ; Value: MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; ""; "SELECT \\"PrimaryKey\\", \\"First Name\\", \\"Last Name\\", \\"Company\\", \\"Title\\" FROM Contacts") \]   
\# get them as XML   
Set Variable \[ $xml ; Value: MBS ( "FM.SQL.XMLRecords"; $sql1 ; "people"; "person"; "ID¶First¶Last¶Company¶Title"; 2+1) \]   
\#   
\# loop over records to look for related record   
Set Variable \[ $count ; Value: MBS ( "[FM.SQL.RowCount](FMSQLRowCount.md)"; $sql1 ) \]   
Set Variable \[ $index ; Value: 0 \]   
If \[ $index ≤ $count \]   
 Loop  
 # get primary key from result  
 Set Variable \[ $key ; Value: MBS ( "[FM.SQL.Field](FMSQLField.md)"; $sql1 ; $index ; 0 ) \]   
 # Run query for related records  
 Set Variable \[ $sql2 ; Value: MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; ""; "SELECT Type, Number FROM \\"Phone Numbers\\" WHERE ForeignKey = ?"; $key ) \]   
 # get them as XML  
 Set Variable \[ $xml2 ; Value: MBS ( "FM.SQL.XMLRecords"; $sql2 ; "phones"; "phone"; "Type¶Number"; 2+1) \]   
 # Now insert them in the person node as new child phones  
 Set Variable \[ $xml ; Value: MBS ( "[XML.SetPathXML](XMLSetPathXML.md)"; $xml ; "people.person\[" &amp; $index &amp;"\].phones"; 0; $xml2 ) \]   
 # don't forget to release memory  
 Set Variable \[ $r ; Value: MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $sql2 ) \]   
 #   
 # next  
 Set Variable \[ $index ; Value: $index + 1 \]   
 Exit Loop If \[ $index ≥ $count \]   
 End Loop  
End If  
\#   
\# don't forget to release memory   
Set Variable \[ $r ; Value: MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $sql1 ) \]   
\# Output the XML with format &amp; color   
Set Field \[ Contacts::XML ; MBS ("[XML.Colorize](XMLColorize.md)"; MBS ("[XML.Format](XMLFormat.md)"; $xml )) \]  
### See also

- [FM.SQL.Execute](FMSQLExecute.md)
- [FM.SQL.Field](FMSQLField.md)
- [FM.SQL.JSONRecord](FMSQLJSONRecord.md)
- [FM.SQL.JSONRecords](FMSQLJSONRecords.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [FM.SQL.RowCount](FMSQLRowCount.md)
- [SQL.Execute](SQLExecute.md)
- [XML.Colorize](XMLColorize.md)
- [XML.Format](XMLFormat.md)
- [XML.SetPathXML](XMLSetPathXML.md)

### Release notes

- **Version 14.2**
    - Added firstRow and lastRow parameter to [FM.SQL.JSONRecords](https://www.mbsplugins.eu/FMSQLJSONRecords.shtml) and [FM.SQL.XMLRecords](https://www.mbsplugins.eu/FMSQLXMLRecords.shtml) functions.

### Blog Entries

- [MBS FileMaker Advent calendar - Door 21 - XML](https://www.mbsplugins.de/archive/2025-12-21/MBS_FileMaker_Advent_calendar_/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 14.2pr5](https://www.mbsplugins.de/archive/2024-05-08/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [What is new in the MBS FileMaker Plugin Version 10.3](https://www.mbsplugins.de/archive/2020-07-31/What_is_new_in_the_MBS_FileMak/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)

This function checks for a license.

Created 29th June 2020 , last changed 6th March 2026

  
[FM.SQL.XMLRecord](FMSQLXMLRecord.md) - [FM.ScriptIDForScriptName](FMScriptIDForScriptName.md)

[HTML Version](FMSQLXMLRecords.shtml)