{"id":1674,"date":"2014-06-25T09:44:47","date_gmt":"2014-06-25T09:44:47","guid":{"rendered":"http:\/\/www.isunshare.com\/blog\/?p=1674"},"modified":"2023-04-17T03:27:47","modified_gmt":"2023-04-17T03:27:47","slug":"lock-and-unlock-record-in-sql-server-database","status":"publish","type":"post","link":"https:\/\/www.isunshare.com\/blog\/lock-and-unlock-record-in-sql-server-database\/","title":{"rendered":"Lock and Unlock Record in SQL Server Database"},"content":{"rendered":"<p>Why we <a href=\"https:\/\/www.isunshare.com\/blog\/lock-and-unlock-record-in-sql-server-database\/\"><strong>lock and unlock record in SQL Server Database<\/strong><\/a>? And how do we lock and unlock records as we will?<\/p>\n<h2>1. Lock Record in SQL Server Database<\/h2>\n<p>\u201cI want to insert a record into permanent table but I don&#8217;t want anyone (any other session) to have access to that record until I allow it. I cannot use transaction for this case. Will sp_getapplock work? If yes, how to code this?\u201d<\/p>\n<p>This is one kind situation when we wish to lock record in SQL Server tables. And it is the most important reason to lock record.<\/p>\n<p>Now we suppose there are four tables in SQL Server database. And we are ready to add row into table 4, so we would not like any influence when adding row. The following code is used to lock table4 and record.<\/p>\n<p><em>insert a row into table4<\/em><br \/>\n<em> while<\/em><br \/>\n<em> begin<\/em><br \/>\n<em> begin transaction<\/em><br \/>\n<em> select * from table4 WITH(ROWLOCK, HOLDLOCK) &#8212;- locking only the new record<\/em><br \/>\n<em> update table1<\/em><br \/>\n<em> update table2<\/em><br \/>\n<em> update table3<\/em><br \/>\n<em> update table4 <\/em><br \/>\n<em> commit transaction<\/em><br \/>\n<em> fetch next<\/em><br \/>\n<em> end<\/em><\/p>\n<h2>2. How to Unlock Locked Row in SQL Server Database<\/h2>\n<p>There are several commands on the tables in SQL Server database. If an application times out or something goes wrong on the server, a lock would be mad on a table, causing problems for other users when they are trying to access records on the tables.<\/p>\n<p>Now take Transaction &#8211; SQL to <strong>unlock record in SQL Server database<\/strong>.<\/p>\n<p><strong>UNLOCK [RECORD nRecordNumber]\u00a0 [IN nWorkArea | cTableAlias]\u00a0\u00a0 [ALL]<\/strong><\/p>\n<p><strong>RECORD <em>nRecordNumber<\/em>:<\/strong> Releases the record lock on record number <em>nRecordNumber<\/em>.<\/p>\n<p><strong>IN <em>nWorkArea<\/em> | <em>cTableAlias<\/em>:<\/strong><\/p>\n<p>Releases a record lock (or locks) or a file lock from a table in a specific work area. <em>nWorkArea <\/em>specifies a work area number and <em>cTableAlias<\/em> specifies a table alias. If you don&#8217;t include <em>nWorkArea<\/em> or <em>cTableAlias<\/em>, UNLOCK releases a record lock (or locks) or a file lock from the table in the currently selected work area.<\/p>\n<p><strong>Related Articles:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.isunshare.com\/blog\/sql-server-2014-product-key-finder\/\">SQL Server 2014 Product Key Finder<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Why we lock and unlock record in SQL Server Database? And how do we lock and unlock records&#8230;<\/p>\n","protected":false},"author":1,"featured_media":126334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140],"tags":[],"class_list":["post-1674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/posts\/1674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/comments?post=1674"}],"version-history":[{"count":4,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/posts\/1674\/revisions"}],"predecessor-version":[{"id":1736,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/posts\/1674\/revisions\/1736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/media\/126334"}],"wp:attachment":[{"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/media?parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/categories?post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.isunshare.com\/blog\/wp-json\/wp\/v2\/tags?post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}