Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Lock a table without using the primary key

$
0
0

Hi,

I am trying to update a table, for example product table,

product (
idint (identity),
product_codenvarchar(50),
descriptionnvarchar(128)
)

the primary key is product_code, during updating the table, I lock the record using updlock, rowlock

select @product_code=product_code from product with(updlock, rowlock) where id=@product_id;

update product set description='ABC' where id=@product_id;

When I do a testing, the record that is locked is not only the record with id that I select (the id is identity column, which is unique), I don't understand how the record locking works, anyone can explain?

If I try to lock using the product_code, which is the primary key, it only lock the record that I want, am I have to use the primary key to lock the record?

Regards,

Budi


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>