{"title":"Query.SaveAsCsv(Integer, Text [, Integer] [, Text]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/query/query-saveascsv-integer-string-integer-string-method/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/query/query-saveascsv-integer-string-integer-string-method, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/query/query-saveascsv-integer-string-integer-string-method","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Saves the resulting data set of a query as a comma-separated values (CSV) file.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"Query.SaveAsCsv(Integer, Text [, Integer] [, Text]) Method","slug":"querysaveascsvinteger-text-integer-text-method","at":0,"chars":4774},{"level":2,"title":"Syntax","slug":"syntax","at":273,"chars":124},{"level":2,"title":"Parameters","slug":"parameters","at":397,"chars":1072},{"level":2,"title":"Return Value","slug":"return-value","at":1469,"chars":233},{"level":2,"title":"Remarks","slug":"remarks","at":1702,"chars":1654},{"level":2,"title":"Example","slug":"example","at":3356,"chars":1285},{"level":2,"title":"Related information","slug":"related-information","at":4641,"chars":133}],"text":"# Query.SaveAsCsv(Integer, Text [, Integer] [, Text]) Method\n> Version: _Available or changed with runtime version 1.0._\nSaves the resulting data set of a query as a comma-separated values (CSV) file.\n> NOTE\n> This method is supported only in Business Central on-premises.\n## Syntax\n```al\n[Ok := ]  Query.SaveAsCsv(Number: Integer, FileName: Text [, Format: Integer] [, FormatArgument: Text])\n```\n## Parameters\nNumber\nType: ../integer/integer-data-type\nThe ID of the query object. If the query that you specify does not exist, then a run-time error occurs.\nFileName\nType: ../text/text-data-type\nThe path and name of the file that you want to save the query data set to.\n*[Optional] Format*\nType: ../integer/integer-data-type\nSpecifies whether the columns of the resulting data set are at fixed positions in the CSV file or separated only by a delimiter.\n*[Optional] FormatArgument*\nType: ../text/text-data-type\nYou set the FormatArgument parameter based on the setting of the Format parameter. If the Format parameter is set to 0, then the FormatArgument parameter specifies the starting position of each column in the data set. The value is a comma separated string of integers that includes an integer for every column. In a CSV file, each line is evenly divided into positions for holding characters. The first integer corresponds to the starting position of the first column, the second integer corresponds to the starting position of the second column, and so on.\n## Return Value\n*[Optional] Ok*\nType: ../boolean/boolean-data-type\ntrue if the SaveAsCsv succeeded, otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.\n## Remarks\nA CSV file stores data in a plain text format. When you save a query dataset as a CSV file, each row of the dataset is stored on a separate line in the file, and each column in a row is separated by a comma or another delimiter of your choice. The first line of the file will contain the column names of the query. A query column name is specified by its Name Property<../../properties/devenv-properties>.\nWhen the SaveAsCSV method is called the query dataset is generated and then saved in CSV format to the file that is designated by the FileName parameter.\nTo apply filters with the SetFilter and SetRange methods, the SaveAsCSV method must be called after SetFilter and SetRange methods, as shown in the following example.\n```\n// Sets a filter on the Quantity column of the query\nQuery.SetFilter(Quantity, '>50');\n\n// Opens a new query that is filtered by the SetFilter method and saves the dataset\nQuery.SaveAsCSV('c:\\test.csv');\n```\nThe SaveAsCSV method can be called at any place in the code and does not require that the Close, Open or Read methods are called before it. When the SaveAsCSV method is called, a new instance of the query is created. The query is implicitly opened, read, and closed. If there is currently a dataset in the opened state when the SaveAsCSV method is called, then that instance is closed. This means that the following code is illegal because the query is not open on the second Read call.\n```al\nQuery.Open;\nQuery.Read;\nQuery.SaveAsCSV('c:\\test.csv');\nQuery.Read;\n```\nThe correct code for this example is as follows.\n```al\nQuery.Open;\nQuery.Read;\nQuery.SaveAsCSV('c:\\test.csv');\nQuery.Open;\nQuery.Read;\n```\n## Example\nThe following example shows how to save the dataset of a query with the name My Customers Query as a CSV file. The file is given the name mycustomers.csv and is saved on the c: drive of the computer running Dynamics 365 Business Central service. The query consists of three columns: No., Name, and City. The file is set to place the columns at the following positions: 1, 10, and 40.\nThis example requires that you create a query called My Customer Query that is based on table 18 Customer and contains the No, Name, and City columns.\n```al\nvar\n  MyCustomerQuery: Query \"My Customer Query\";\n  OK: Boolean;\n  Text000: Label 'Query was not saved.';\nbegin\n    OK := MyCustomerQuery.SaveAsCSV('c:\\mycustomers.csv', 0, '1,10,40');\n    if not OK then\n      Error(Text000);\nend;\n```\nThe following code shows an example of the content of the saved file.\n```\nNo       Name                          City\n01121212 Spotsmeyer's Furnishings      Miami\n01445544 Progressive Home Furnishings  Chicago\n01454545 New Concepts Furniture        Atlanta\n01905893 Candoxy Canada Inc.           Thunder Bay\n01905899 Elkhorn Airport               Elkhorn\n01905902 London Candoxy Storage Campus London\n\n```\nIf for some reason the file cannot be saved, then the message Query was not saved. appears.\n## Related information\nquery-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}