SQL Server transaction and SELECT statement

前端 未结 2 1026

I often saw many people use SELECT statement within a transaction. I often use insert/update/delete only in transaction. I just do not understand t

2条回答
  •  悲&欢浪女
    2020-12-29 07:24

    try doing this and you will understand:

    Open a two new queries on SSMS (lets call it A and B from now one) and on A, create a simple table like this:

    create table transTest(id int)
    insert into transTest values(1)
    

    now, do the following:

    do select * from transTest in both of them. You will see the value 1

    On A run:

    set transaction isolation level read committed
    

    On B run:

    begin transaction
    insert into transTest values(2)
    

    On A run:

    select * from transTest

    you will see that the query wont finish because it is locked by the transaction on A

    On B run:

    commit transaction
    

    Go back to A and you will see that the query finished

    Repeat the test with set transaction isolation level read uncommitted on A you will see that the query wont be locked by the transaction

提交回复
热议问题