Domanda

I have an SSIS Package that truncate and load tables every day, I need to make sure that no one can access those tables while they are being updated... because he might get partial data.

What is the best method to do that?

È stato utile?

Soluzione

It sounds like you need to use an exclusive table lock, start your transaction with the following code (feel free to correct any syntax errors as I'm a postgres/oracle guy)

SET TRANSACTION ISOLATION LEVEL TABLOCKX
GO
BEGIN TRANSACTION

do your work here. then commit or rollback.

for more information see this msdn article

Altri suggerimenti

You can use HOLDLOCK or DBLOCK as well

SQL SERVER – Locking Hints

ROWLOCK Use row-level locks when reading or modifying data.

PAGLOCK Use page-level locks when reading or modifying data.

TABLOCK Use a table lock when reading or modifying data.

DBLOCK Use a database lock when reading or modifying data.

UPDLOCK UPDLOCK reads data without blocking other readers, and update it later with the assurance that the data has not changed since last read.

XLOCK Use exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction.

HOLDLOCK Use a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required.

NOLOCK This does not lock any object. This is the default for SELECT operations. It does not apply to INSERT, UPDATE, and DELETE statements.

Examples:
SELECT OrderID
FROM Orders WITH (ROWLOCK)
WHERE OrderID BETWEEN 100 AND 2000

 UPDATE Products WITH (NOLOCK)
 SET ProductCat = 'Machine'
 WHERE ProductSubCat = 'Mac'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top