{"title":"Query.TopNumberOfRows([Integer]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/query/queryinstance-topnumberofrows-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/queryinstance-topnumberofrows-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/queryinstance-topnumberofrows-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":"Specifies the maximum number of rows to include in the resulting data set of a query.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"Query.TopNumberOfRows([Integer]) Method","slug":"querytopnumberofrowsinteger-method","at":0,"chars":3690},{"level":2,"title":"Syntax","slug":"syntax","at":188,"chars":147},{"level":2,"title":"Parameters","slug":"parameters","at":335,"chars":386},{"level":2,"title":"Return Value","slug":"return-value","at":721,"chars":151},{"level":2,"title":"Remarks","slug":"remarks","at":872,"chars":552},{"level":2,"title":"Example","slug":"example","at":1424,"chars":1914},{"level":2,"title":"Related information","slug":"related-information","at":3338,"chars":352}],"text":"# Query.TopNumberOfRows([Integer]) Method\n> Version: _Available or changed with runtime version 1.0._\nSpecifies the maximum number of rows to include in the resulting data set of a query.\n## Syntax\n```al\n[CurrentRows := ]  Query.TopNumberOfRows([NewRows: Integer])\n```\n> NOTE\n> This method can be invoked using property access syntax.\n## Parameters\nQuery\nType: query-data-type\nAn instance of the query-data-type data type.\n*[Optional] NewRows*\nType: ../integer/integer-data-type\nThe number of rows to include in the resulting data set. If you do not set the NewRows parameter, then the resulting data set will include all rows. If you set the value to 0, then there is no limit and all rows of the data set are returned.\n## Return Value\n*[Optional] CurrentRows*\nType: ../integer/integer-data-type\nGets the current maximum number of rows included in the resulting data set\n## Remarks\nYou use the TopNumberOfRows method to limit the resulting dataset to the first set of rows that are generated for the query. For example, you can include only the first 10 or first 100 rows in the resulting dataset. The TopNumberOfRows method is useful for key performance indicators such as the top number of customers or sales.\nYou can also specify the number of rows to include in the dataset by setting the ../../properties/devenv-topnumberofrows-property. The TopNumberOfRows method will overwrite the TopNumberOfRows property setting.\n## Example\nThis code example demonstrates how to use the TopNumberOfRows method on a query to return the top 10 customer sales orders based on the quantity of items.\nThe following query object links table 18 Customer and table 37 Sales Line and uses the TopNumberOfRows property to get top 5 customer sales orders based on the quantity of items.\n```al\nquery 50123 \"Customer_Sales_Quantity\"\n{\n    QueryType = Normal;\n    // Sets the resultS to include the top 5 the results in descending order\n    TopNumberOfRows = 5;\n    OrderBy = descending(Qty);\n\n    elements\n    {\n        dataitem(C; Customer)\n        {\n            column(Customer_Number; \"No.\")\n            {\n            }\n\n            column(Customer_Name; Name)\n            {\n            }\n\n            dataitem(SL; \"Sales Line\")\n            {\n                DataItemLink = \"Sell-to Customer No.\" = c.\"No.\";\n                SqlJoinType = InnerJoin;\n\n                column(Qty; Quantity)\n                {\n                }\n            }\n        }\n    }\n}\n```\nThe following codeunit runs the query, saves it as a CSV file, and displays a message that states the number of rows that are returned in the resulting dataset.\n```al\ncodeunit 50100 MyQueryTop10\n{\n    trigger OnRun()\n    begin\n\n        // Overwrites the TopNumberOfRows property and returns the first 10 rows in the dataset.\n        //MyQuery.TopNumberOfRows(10);\n        // Opens the query.\n        MyQuery.Open;\n        // Reads each row of the dataset and counts the number of rows.\n        while MyQuery.Read do begin\n            Counter += 1;\n        end;\n        // Saves the dataset as a CSV file.\n        MyQuery.SaveAsCsv('c:\\temp\\CustomerSales.csv');\n        // Displays a message that shows the number of rows.\n        Message(Text000, counter);\n\n    end;\n\n    var\n        MyQuery: Query \"Customer_Sales_Quantity\";\n        Counter: Integer;\n        Text000: Label 'count %1.';\n}\n```\n## Related information\nquery-data-type\n../../devenv-query-object\nLinking and Joining Data Items<../../devenv-query-links-joins>\nAggregating Data in Query Objects<../../devenv-query-totals-grouping>\nFiltering Data in Query Objects<../../devenv-query-filters>\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}