How to explicitly lock a table in Microsoft SQL Server (looking for a hack - uncooperative client)

后端 未结 3 859
礼貌的吻别
礼貌的吻别 2021-01-05 00:38

This was my original question:

I am trying to figure out how to enforce EXCLUSIVE table locks in SQL Server. I need to work around uncooperative readers (beyond m

3条回答
  •  梦谈多话
    2021-01-05 00:52

    There's no direct way to force locking when a connection is in the READ UNCOMMITTED isolation level.

    A solution would be to create views over the tables being read that supply the READCOMMITTED table hint. If you control the table names used by the reader, this could be pretty straightforward. Otherwise, you'll have quite a chore as you'll have to either modify writers to write to new tables or create INSTEAD OF INSERT/UPDATE triggers on the views.

    Edit:

    Michael Fredrickson is correct in pointing out that a view simply defined as a select from a base table with a table hint wouldn't require any trigger definitions to be updatable. If you were to rename the existing problematic tables and replace them with views, the third-party client ought to be none the wiser.

提交回复
热议问题