0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQL Serverでトランザクションのロックを解放する

0
Posted at

1. はじめに

  • SQL Serverでは、トランザクションが実行される際にデータベースのリソースに対してロックがかかります
  • 他のトランザクションが待機状態になり、デッドロックを回避するため強制的にロックを開放する必要があります

2. 開発環境

  • SQL Server 2022

3. ロックを確認するSQL

  • SQL Serverで現在のロック状況を確認するためのSQLクエリは以下の通りです。
SELECT
  OBJECT_NAME( t2.object_id ) AS tableName
 ,resource_type AS type
 ,request_session_id AS sessionId
FROM
  sys.dm_tran_locks t1
INNER JOIN sys.partitions t2
  ON t1.resource_associated_entity_id = t2.hobt_id
WHERE
  OBJECT_NAME( t2.object_id ) = 'TABLENAME';   -- ここに接続を切りたいテーブル名を記述する
;
  • 実行結果
    image.png

4. ロックを解放するSQL

  • ロックを解放するためには、特定のセッションIDを終了させることができます。
kill セッションID;  -- 上記のSQLで特定したセッションを終了する
  • 実行結果
    image.png

5. 参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?