简体   繁体   中英

Does every statement in begin end starts new transaction in SQL SERVER?

For example, I have this code in my sql server procedure

CREATE PROCEDURE Test

Begin

Update table1
set col1 = 'a'

Update table2
set col2 = 15

select * from table3 

delete from table4
where col4 = 5;

end

When I exec test procedure, how many transactions I have? Does begin end creates a new transaction? if not Will I have 4 implicit transactions?

BEGIN doesn't start a transaction no. BEGIN TRANSACTION does.

All BEGIN...END does on is own, really, is denote a code group. BEGIN...END (Transact-SQL) :

Encloses a series of Transact-SQL statements so that a group of Transact-SQL statements can be executed. BEGIN and END are control-of-flow language keywords.

If you want to have an explicit transaction, you need to use BEGIN TRANSACTION and COMMIT / ROLLBACK . I suggest having a look at Transactions (Transact-sQL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM