Say I am running a query
begin tran
update users
set name = 'Jimmy'
where name = 'john'
If I DON'T rollback the transaction, will these changes still be made, will it throw an error or will it act as a rollback anyway?
Say I am running a query
begin tran
update users
set name = 'Jimmy'
where name = 'john'
If I DON'T rollback the transaction, will these changes still be made, will it throw an error or will it act as a rollback anyway?
You have to understand what a transaction is - It is a single unit of work. It is ALL or NOTHING (follows ACID properties) and this guarantees the database consistency.
If I DON'T rollback the transaction, will these changes still be made
The changes will be made only if you commit. SQL server will record all the changes to transaction log and once you commit, they are hardened to the data file.
If you don't commit, then your transaction will remain OPEN indefinitely - which you can see in sys.dm_tran_active_transactions / sys.dm_tran_database_transactions / sys.dm_tran_session_transactions
or using DBCC OPENTRAN
.
Also, what you have initiated is an explicit transaction, which should be explicitly ended with a COMMIT or ROLLBACK statement.
Also read - Is it a bad practice to always create a transaction?
If you neither commit nor rollback the transaction, the transaction will continue to exist indefinitely. It will continue to hold its locks, potentially blocking other sessions, until either you end the transaction via a commit
or a rollback
or until a DBA comes along and kills the session (or until something like a network hiccup causes the connection to fail). If a DBA kills the session, they will implicitly issue a rollback for this and any other open transactions.
Changes won't be made until and unless the transaction is committed , its all or nothing as mentioned above answers ,
Atomicity is the reason behind this , You can check ACID transaction properties , its the fundamental properties that a database system follow .