Runs a query object and generates a dataset that can be read.

The following code shows the syntax of the OPEN function. Query is a variable of the Query data type that specifies the query object.

[Ok :=] Query.OPEN

Return Value

Type: Boolean

true if the query was opened without errors; otherwise, false.

If you omit this optional return value and if the query does not execute successfully, then a run-time error occurs. If you include a return value, then it is assumed that you will handle any errors and no run-time error occurs when the OPEN function is called. However, a run-time error will occur if the READ function is called afterward.


When the OPEN function is called, the query is executed and the Query variable is put in a state in which the resulting dataset can be read by the READ function. The OPEN function has the following behavior:

  • To apply filters for the SETFILTERS function or SETRANGE function, you call the SETFILTERS function or SETRANGE function before the OPEN function.
  • To read a row from the dataset, you must call the OPEN function before the READ function, as shown in the following code example.
    Copy Code
    Query.SETFILTER(Column1, String);
  • To close an open query and return it to the initialized state, you can call the CLOSE function. However, you can call the OPEN function multiple times without calling the CLOSE function because the OPEN function implicitly calls the CLOSE function if the query dataset is currently in an opened state.
    • If the OPEN function is called on a query that is already in the opened or in the reading state, then the query dataset is closed, and then the query is executed again. To continue to loop through the dataset, the READ function must be called again. The next READ function call returns the first row in the dataset, as shown in the following code example.
      Copy Code
      // Opens the query and generates a dataset.
      // Closes the query and reopens it.
      // Reads the first row in the new dataset.
    • OPEN function does not clear any filters that were set by the SETFILTER or SETRANGE functions on a previous OPEN call. If you want to clear the filters, then you must call the CLEAR function on the query variable.
      Copy Code
      Query.SETFILTER(Column1, String);
  • You are required to call the OPEN function before the SAVEASXML Function or SAVEASCSV Function. The SAVEASXML and SAVEASCSV functions automatically close the current query dataset and initialize a new instance of the query.


The following example demonstrates how to use the OPEN function on a query. The example code sets filters on the query, opens the query, and then reads the dataset. For each row in the dataset, a message box is displayed that contains the values of the columns in the row.

This example requires that you do the following:

  1. Create a query called Customer_SalesQuantity that links table 18 Customer with table 37 Sales Lines from the CRONUS International Ltd. demonstration database. Include columns for the Name and No. fields from the Customer table and the Quantity field from Sales Lines table.
    For step-by-step instructions for creating this query, see Walkthrough: Creating a Query to Link Two Tables.
  2. Create the following variable and text constant.

    Variable name DataType Subtype




    Text constant name ENU Value


    Customer name = %1, Quantity = %2

The following C/AL code opens the query, reads each row of the dataset, and then displays a message that uses the content of the row. You can add the code to a codeunit, and then run the codeunit to see the results.

Copy Code
// Sets a filter to display only sales quantities greater than 20.
MyQuery.SETFILTER(Quantity, '>20'); 
// Runs the query.
// Reads each row in the dataset and displays a message with column values.
// Stops reading when there are no more rows remaining in the dataset (READ is FALSE).
  MESSAGE(Text000, MyQuery.Name, MyQuery.Quantity);

When the code is run, a message that resembles the following appears for each row in the dataset:

Customer name = The Device Shop, Quantity = 30

See Also