sqlsql-server-2005table-lock

Persistent SQL Table lock from C#


I'm trying to create a persistent SQL (SQL Server 2005) lock on a table level. I'm not updating/querying the specified table, but I need to prevent a third party application from updating the locked table as a means to prevent transactions from being posted (the table I wish to lock is the key on their transaction that interferes with my processing).

From my experience the table is only locked for the time a specific transaction is taking place. Any ideas?

The 3rd party developer has logged this feature as an enhancement, but since they are in the middle of rolling out a major release I can expect to wait at least 6 months for this. I know that this isn't a great solution, since their software will fall over but it is of a critical enough nature that we're willing to live with the consequences.


Solution

  • Move the table to a different file group, then alter the file group to read-only. The table will be in effect read-only:

    ALTER DATABASE dbName 
       ADD FILEGROUP ReadOnlyFG;
    GO
    
    ALTER DATABASE dbName 
       ADD FILE (
        NAME = ...,
        FILENAME = '...')
       TO FILEGROUP ReadOnlyFG;
    GO
    
    ALTER TABLE tableName MOVE TO ReadOnlyFG;
    GO
    
    ALTER DATABASE dbName
       MODIFY FILEGROUP ReadOnlyFG READONLY;