{"title":"RecordRef.LockTable([Boolean] [, Boolean]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/recordref/recordref-locktable-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/recordref/recordref-locktable-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/recordref/recordref-locktable-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":"Starts locking on a table to protect it from write transactions that conflict with each other.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"RecordRef.LockTable([Boolean] [, Boolean]) Method","slug":"recordreflocktableboolean-boolean-method","at":0,"chars":4505},{"level":2,"title":"Syntax","slug":"syntax","at":207,"chars":84},{"level":2,"title":"Parameters","slug":"parameters","at":291,"chars":663},{"level":2,"title":"Remarks","slug":"remarks","at":954,"chars":1374},{"level":2,"title":"Example 1","slug":"example-1","at":2328,"chars":936},{"level":2,"title":"Example 2","slug":"example-2","at":3264,"chars":1002},{"level":2,"title":"Related information","slug":"related-information","at":4266,"chars":239}],"text":"# RecordRef.LockTable([Boolean] [, Boolean]) Method\n> Version: _Available or changed with runtime version 1.0._\nStarts locking on a table to protect it from write transactions that conflict with each other.\n## Syntax\n```al\n RecordRef.LockTable([Wait: Boolean] [, VersionCheck: Boolean])\n```\n## Parameters\nRecordRef\nType: recordref-data-type\nAn instance of the recordref-data-type data type.\n*[Optional] Wait*\nType: ../boolean/boolean-data-type\nSpecifies what to do if the table is already locked. If this parameter is true and if another application has already locked the table, the system will wait until the table is unlocked. If this parameter is false and if another application has already locked the table, a run-time error occurs.\n*[Optional] VersionCheck*\nType: ../boolean/boolean-data-type\nIf this parameter is true, the version of the RecordRef will be checked. If this parameter is false, blank, or not used, the version will not be checked.\n## Remarks\nRead scale-out lets databases offload read-only workloads to read-only replicas, isolating them from the main read-write workload to improve performance. See Using read scale-out for better performance<../../../administration/database-read-scale-out-overview>.\nLockTable behaves differently depending on whether read scale-out is used. If the session doesn't use read scale-out, LockTable does these actions:\n1. Starts a transaction.\n1. Makes sure that all subsequent statements that read data apply an UPDLOCK on the database.\nIf the session uses Read Scale-Out, LockTable does the following:\n1. Makes sure that all subsequent statements that read data use REPEATABLEREAD on the database.\nBecause all write operations automatically lock the table that is being used, LockTable would appear unnecessary. However, you could have a transaction in which an application wants to inspect data before possibly changing it, with a guarantee that the data being changed has not been modified by other applications since the read operation. The solution is to explicitly lock the table before the read operation. This makes sure that no other application makes changes between the read operation and the possible write operation.\nThe table lock is released (unlocked) when the transaction is committed.\nThis method works the same as the ../record/record-locktable-method.\n## Example 1\nThe following example opens table number 18 \\(Customer\\) as a RecordRef that is named MyRecordRef. The LockTable method locks the table. This ensures that no records are inserted or deleted during the counting process. The recordref-count-method then retrieves the number of records in the table. The number of records is stored in the Count variable. The name of the table and the number of records in the table is displayed in a message box. The varTableNo variable can be used to open any table and get the number of records in that table by changing the value of the varTableNo variable.\n```al\nvar\n    CustomerRecref: RecordRef;\n    Count: Integer;\n    varTableNo: Integer;\n    Text000: Label 'The number of records in the %1 table is: %2.';\nbegin\n    varTableNo := 18;\n    MyRecordRef.Open(varTableNo);\n    MyRecordRef.LockTable;\n    Count := MyRecordRef.Count;\n    Message(Text000, MyRecordRef.Name, Count);\nend;\n```\n## Example 2\nThis example uses pseudo-language to show the scope of write locks. Both an explicit lock and an automatic lock are illustrated. The first line \\(1\\) explicitly locks table A. If this explicit lock was not set on table A, the Database Management System \\(DBMS\\) would automatically lock this table when a record is inserted \\(3\\). Table B is not locked explicitly, but is locked automatically by the DBMS when a record is inserted \\(4\\). Both locks are active until the system exits the AL code module \\(5\\).\n```\nBeginWriteTransaction\nTableA.LockTable // (1)\nFindRec(TableA, ...) // (2)\n.\n.\nInsertRec(TableA,...) // (3)\n.\nInsertRec(TableB) // (4)\n.\n.\nEndWriteTransaction // (5)\n```\nIf a data update depends on a prior read operation and there is a long time between the read operation and the write operation, you may not want to lock the table as you usually would during a transaction. This enables you to prevent other users from updating the table until your transaction is committed.\n## Related information\nrecordref-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>\nUsing read scale-out for better performance<../../../administration/database-read-scale-out-overview>"}