{"title":"Using Queries Instead of Record Variables","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-query-using-instead-record-variables/","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/devenv-query-using-instead-record-variables, 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/devenv-query-using-instead-record-variables","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","date":"2021-04-01","meta":{"topic":"concept-article"},"outline":[{"level":1,"title":"Using Queries Instead of Record Variables","slug":"using-queries-instead-of-record-variables","at":0,"chars":4641},{"level":2,"title":"Code Example Using Record Variables","slug":"code-example-using-record-variables","at":310,"chars":1629},{"level":2,"title":"Corresponding Query Implementation","slug":"corresponding-query-implementation","at":1939,"chars":2201},{"level":2,"title":"Related information","slug":"related-information","at":4140,"chars":501}],"text":"# Using Queries Instead of Record Variables\nIn scenarios where you want to read records from multiple tables, it can be a good idea to use a query instead of implementing code with record variables. Using a query can improve performance and also simplify the AL code that is required to perform the operation.\n## Code Example Using Record Variables\nThe following AL code shows an example of using record variables to retrieve and handle records from two tables. You could potentially use this code to track item movement. The code uses two record variables, Item and ItemLedgerEntry, to retrieve the first five records from table 27 Item and table 32 Item Ledger Entry where the Entry Type field equals Sale. The retrieved records are passed to and handled by the OutputData method.\n```al\nbegin\n    count := 0;\n    if Item.FINDSET then\n        repeat\n            PrevDate := 0D;\n            TotalQty := 0;\n            ItemLedgerEntry.SETCURRENTKEY(\"Item No.\", \"Posting Date\");\n            ItemLedgerEntry.SETRANGE(\"Item No.\", Item.\"No.\");\n            ItemLedgerEntry.SETRANGE(\"Entry Type\",\n                ItemLedgerEntry.\"Entry Type\"::Sale);\n            if ItemLedgerEntry.FINDSET then\n                repeat\n                    if (ItemLedgerEntry.\"Posting Date\" <> PrevDate) and (PrevDate <> 0D) then begin\n                        OutputData(1, Item.\"No.\", Item.Description, PrevDate, -TotalQty);\n                        TotalQty := 0;\n                        count := count + 1;\n                    end;\n                    PrevDate := ItemLedgerEntry.\"Posting Date\";\n                    TotalQty := TotalQty + ItemLedgerEntry.Quantity;\n                until (ItemLedgerEntry.NEXT = 0) or (count >= 4);\n            if PrevDate <> 0D then begin\n                OutputData(1, Item.\"No.\", Item.Description, PrevDate, -TotalQty);\n                count := count + 1;\n            end;\n        until (Item.NEXT = 0) or (count >= 4);\nend;\n```\n## Corresponding Query Implementation\nThe following AL code represents a query object and additional code that will return the same results as the previous example that uses record variables.\n```al\nquery 50100 \"Item Movements Query\"\n{\n    QueryType = Normal;\n\n    elements\n    {\n        // This dataitem corresponds to the `Item` record variable in the record variable example.\n        dataitem(Item; Item)\n        {\n            column(No_; \"No.\")\n            {\n            }\n            column(Description; Description)\n            {\n            }\n\n            // This dataitem corresponds to the `ItemLedgerEntry` record variable in the record variable example.\n            dataitem(Item_Ledger_Entry; \"Item Ledger Entry\")\n            {\n                // The DataItemLink and  SqlJoinType settings correspond to the `ItemLedgerEntry.SETRANGE(\"Item No.\",Item.\"No.\");` statement in the record variable example.\n                DataItemLink = \"Item No.\" = Item.\"No.\";\n                SqlJoinType = InnerJoin;\n\n                filter(Entry_Type; \"Entry Type\")\n                {\n                }\n                column(Posting_Date; \"Posting Date\")\n                {\n                }\n\n                // The SUM corresponds to the `TotalQty := TotalQty + ItemLedgerEntry.Quantity;` statement in the record variable example.\n                column(Sum_Quantity; Quantity)\n                {\n                    Method = Sum;\n                }\n            }\n        }\n    }\n}\n```\nAdd the following code to a codeunit that will run the query.\n```al\nvar\n    ItemMovements: Query \"Item Movements\"\nbegin\n    ItemMovements.TopNumberOfRows(5);\n    ItemMovements.SetRange(Entry_Type,ItemMovements.Entry_Type::Sale);\n    ItemMovements.Open;\n    while ItemMovements.Read do\n      OutputData(2, ItemMovements.Item_No,ItemMovements.Description,ItemMovements.Posting_Date,ItemMovements.Sum_Quantity);\nend;\n```\nThe `ItemMovements.TOPNUMBEROFROWS(5);` statement will include only the first 5 records in the resulting dataset and corresponds to implementing the count variable in the record-based code example.\nThe OutputData method performs the same operations as the OutputData method in the record variable example.\n## Related information\ndevenv-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>\nmethods-auto/query/queryinstance-setrange-method\nmethods-auto/query/queryinstance-open-method\nmethods-auto/query/queryinstance-topnumberofrows-method\nAPI Query Type<devenv-api-querytype>\nDeveloping Extensions<devenv-dev-overview>\nAL Development Environment<devenv-reference-overview>"}