开发者

Can a generated table be deleted in a transaction to prevent other people from accessing it while its being updated

开发者 https://www.devze.com 2023-03-25 09:38 出处:网络
two relating question: I have a table that is in use in a production or live environment.Is it appropriate and possible to begin a transaction on that table to ensure no one is using that table while

two relating question: I have a table that is in use in a production or live environment. Is it appropriate and possible to begin a transaction on that table to ensure no one is using that table while it is dropped and re-generated and then committed or rolled back so that the users on live side will not notice? It may be better to delete rows and then repopulate rows. 2nd part of the question is how would I go about performing this drop or deletion of 开发者_运维百科row in a transact statement, or should this be done on a tempory database table?

Thanks.


1)to lock all table when updating you can use Table Hints (see http://msdn.microsoft.com/en-us/library/ms187373%28v=SQL.100%29.aspx), for example

USE AdventureWorks;
GO
UPDATE Production.Product
WITH (TABLOCK)
SET ListPrice = ListPrice * 1.10
WHERE ProductNumber LIKE 'BK-%';
GO

2)to delete all rows in table you need to use TRUNCATE TABLE. AS BOL says, TRUNCATE TABLE always locks the table and page but not each row. So, if somebody locks one of the record in this table, you cannot lock this table and TRUNCATE cannot begin.

0

精彩评论

暂无评论...
验证码 换一张
取 消