Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to execute Commit statement after Update in SQL Server

Tags:

sql

I have done some update to my record through SQL Server Manager.

As Update statement is not having explicit commit, I am trying to write it manually.

Update mytable
set status=0;
Commit;

I am getting message as Commit has no begin transaction

like image 760
Patan Avatar asked Sep 26 '13 11:09

Patan


People also ask

Do we need to COMMIT after update in SQL?

You should begin a Transaction before committing. You should open a transaction. Inside it you should perform update then commit changes.

Is COMMIT necessary after update?

It doesn't matter: only full transactions require COMMIT. It literally does not make sense to issue a COMMIT unless or until we have completed a whole business unit of work. This is a key concept. COMMITs don't just release locks.

Do we need to COMMIT after insert in SQL Server?

So yes, by default, if you're just using INSERT , the records you insert will be committed, and there is no point trying to roll them back.

Is update auto-commit?

You cannot use auto-commit if you do any positioned updates or deletes (that is, an update or delete statement with a WHERE CURRENT OF clause) on cursors which have the ResultSet. CLOSE_CURSORS_AT_COMMIT holdability value set. Auto-commit automatically closes cursors that are explicitly opened with the ResultSet.


2 Answers

The SQL Server Management Studio has implicit commit turned on, so all statements that are executed are implicitly commited.

This might be a scary thing if you come from an Oracle background where the default is to not have commands commited automatically, but it's not that much of a problem.

If you still want to use ad-hoc transactions, you can always execute

BEGIN TRANSACTION

within SSMS, and than the system waits for you to commit the data.

If you want to replicate the Oracle behaviour, and start an implicit transaction, whenever some DML/DDL is issued, you can set the SET IMPLICIT_TRANSACTIONS checkbox in

Tools -> Options -> Query Execution -> SQL Server -> ANSI
like image 182
SWeko Avatar answered Oct 11 '22 02:10

SWeko


Sql server unlike oracle does not need commits unless you are using transactions.
Immediatly after your update statement the table will be commited, don't use the commit command in this scenario.

like image 35
Kristof Avatar answered Oct 11 '22 04:10

Kristof