WITH (TABLOCK) in sql server

when can use WITH (TABLOCK) function in sql. its preferable to use WITH (TABLOCK) like nolock. or not
kowsika deviAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
In what context or scenario you are checking? Are you inserting data?
Pawan KumarDatabase ExpertCommented:
Basically when we move data from one table to another it is recommended to use TABLOCK hint on the destination table. This will reduce concurrency because it will immediately take the table lock on the destination table. So this hint will help you in inserting data quickly.

INSERT INTO DestinationTable WITH (TABLOCK) (<Column1, Column2.....>)
SELECT Column1, Column2... FROM Yourtable

Open in new window

If you are loading data then I recommend you to also read the Performance  guide for data loading. <https://msdn.microsoft.com/en-us/library/dd425070.aspx>

It is equal to using READ UNCOMMITED as a transaction isolation level. So, you stand the risk of reading an uncommitted row that is subsequently rolled back, i.e. data that never made it into the database. So, while it can prevent reads being deadlocked by other operations, it comes with a risk. Basically you can get dirty reads. WITH(NOLOCK) - Does not improve performance. WITH(NOLOCK) is NOT by default.

SELECT Column1, Column2... FROM Yourtable WITH (NOLOCK)

Open in new window

So if you are loading data first option WITH (TABLOCK) is good.
kowsika deviAuthor Commented:
hi pawan,

just iam read one site thy used like that
SELECT @count = COUNT(*) FROM dbo.table_name WITH (TABLOCK);  i sqw only nolock the word tablock is diffrent and i put in sql it shows like keyword. for knowing purpose am asking.
y we need to use.
Pawan KumarDatabase ExpertCommented:
Got it..

SELECT @count = COUNT(*) FROM dbo.table_name WITH (TABLOCK)
The above will not help in any case. Performance will not be improved with this. Here we are locking the entire table and getting the data.

SELECT @count = COUNT(*) FROM dbo.table_name WITH (NOLOCK)
This may give us dirty reads. Here we are not putting any lock on the table so we may be dirty reads.

SELECT @count = COUNT(*) FROM dbo.table_name
This should be used.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kowsika deviAuthor Commented:
thanks jj now i understood
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.