views:

265

answers:

1

Hello everyone,

I want to know and pros and cons of setting sp_tableoption of "table lock on bulk load" for SQL Server 2005/2008? My scenario is, I have 3 threads working on database, one query thread, one bulk insert thread and one delete thread.

Here is the MSDN related link for sp_tableoption,

http://msdn.microsoft.com/en-us/library/ms173530.aspx

thanks in advance, George

+2  A: 

just do it in the BULK INSERT instead

examples

Bulk Insert which will lock the table

BULK INSERT Northwind.dbo.[ORDER Details]
   FROM 'f:\orders\lineitem.tbl'
   WITH
     (
        FIELDTERMINATOR = '|',
        ROWTERMINATOR = ':\n',
        FIRE_TRIGGERS,
        TABLOCK  --this is it
      )

This statement will not lock the table

BULK INSERT Northwind.dbo.[ORDER Details]
       FROM 'f:\orders\lineitem.tbl'
       WITH
         (
            FIELDTERMINATOR = '|',
            ROWTERMINATOR = ':\n',
            FIRE_TRIGGERS
          )

BCP with tablock by using the -h (for hint) switch and "TABLOCK"

bcp pubs..authors2 in authors.txt -c -t, -Sservername -Usa -Ppass -h "TABLOCK"

you could do a bulk insert from within SSIS or DTS and to the table lock there

for example in DTS under transform data task look at the options tab, check use fast load and table lock SSIS has something similar

SQLMenace
I am confused. You mentioned -- "just do it in the BULK INSERT instead", but I did not notice you set any tableoption in your bulk insert SQL statement? Could you clarify please?
George2
I do it with the TABLOCK argument in the first query and with -h "TABLOCK" in the second query
SQLMenace
I updated the answer by giving you 2 queries for the BULK INSER one with and one without the TABLOCK
SQLMenace
I am confused. From the MSDN page I referred, by default when doing bulk insert, row level locks will be used. Why you mention tablock here (tablock is table lock)? Do you mean you suggest to use table lock when doing bulk insert?
George2
Another confusion is I think there is no need to use SSIS or DTS to run the command you mentioned, using normal SQL command line tool or Management Studio is also fine, any comments?
George2
yes those two can be executed from the commandline or from SSMS but you need to wrap the bcp in xp_cmdshell. What i meant was you can also do bul insert with an execute sql task you just need to check "use fast load" and "table lock"
SQLMenace
My confusion is why you suggest me to use table level lock (tablock)? I think by default row level lock should be used during bulk insert?
George2
>>I think by default row level lock should be used during bulk insert?No, only with a TABLOCK could you get a minimally logged operation (also depends on other factors)
SQLMenace