Could not succeed in rollback database transactions in SQL Server - sql-server

For the trail purpose to check how to rollback deleted transaction in SQL Server i have created a table in my database and then inserted some records in it. Then i issued delete command to delete these records, after that i applied Rollback command to recover deleted records but could not succeeded
PS: The whole operation is performed within the begin transaction
Here is the scenario:
While i have issued SELECT command to see the records.

Related

Breaking a connection to SQL Server halfway through an execution

If I execute a script against SQL, which has multiple transactions in it, but break the connection half way through, what does SQL do? Does it run the script to completion, or just roll-back the transaction it was currently in (or finish it)?
When SQL Server detects that a connection is broken, it should rollback any current transaction and abort the current batch1.
Any preceding committed transactions will still be committed.
1Here I'm trying to draw the distinction between scripts and batches. Many client tools support scripts containing multiple batches (delimited by GOs) and its the batches that get submitted to SQL Server, sequentially.
Well, depends, if there are multiple transactions in the script SQL, do you put commit into each transaction ? for example, if your script like this
BEGIN TRAN --Transaction 1
INSERT TableA VALUES(value1,value2)
COMMIT TRAN
BEGIN TRAN --Transaction 2
INSERT TableB VALUES(value1,value2)
COMMIT TRAN
if your sql server disconnected, half-way of the execution of the whole script, but while on the way of execution of the script and the sql server has completed transaction 1, and that's committed, then transaction 1 is completed, and you can see the result on tableA, but when the sql server running transaction 2 imidiately and few moment later the transaction 2 yet incomplete and the connection gets down, transaction 2 will be rollbacked, so you cant see the result on TableB
Only committed transactions will be executed.
Other ones will do a roll-back.

ssis transaction with out Msdtc

One of the packages is going to implement using
SQL Server Integration Services SSIS Transactions without MSDTC.
The Execute SQL task has placed ,before the data flow(Df_insert) for begin transaction.There are several update steps and index creation steps ,after this First data flow(Df_Insert).There is an update scripts which is in another sequence container and ,need to be part of this transaction.
Is there any way to include only the Df_insert and the update scripts
in the transaction.
The control flow looks like, the below
From SQL Transaction point of view ALL DML statements, i.e. inserts-updates-deletes, between BEGIN TRAN and COMMIT are part of this transaction and not deducible. Your task - committing only DFT and update script - means that update, update2 and delete are temp data used in your update script and discarded later on.
Approach - rework your logic to move results of update, update2 and possibly delete results into TEMP tables and use it afterwards. Regular #temp_table will be fine since you have to use RetainSameConnection=true for transaction without MSDTC.

CHECKPOINT and begin/commit transaction in updating bulk data in Full recovery mode, SQL server

I am updating two big tables using a single procedure. I wanted to know how to manage commit transaction/checkpoint so that i will minimize the risk of blow of transaction log.
Database is SQL server 2005 and recovery mode is full.
Here is my sample code.
create procedure procedure_name
as
declare #CNT INT;
set #CNT=1;
BEGIN trans
while #CNT>0
update top (50000) table_a
where condition
set #CNT=##rowcount
checkpoint
end;
set #cnt=##rowcount
while #CNT>0
update top (50000) table_a
where condition
set #CNT=##rowcount
checkpoint
end;
commit transaction
One of the many reasons logging exists is to help you rollback your transactions if you don't want to commit them to disk. Starting a transaction, adding a checkpoint in the middle and then committing I doubt will help your cause.
Please have a good read of these posts which cover your query. Change to a Simple recovery model if you do not need point in time recovery? Perform a full backup after the insert if this is a one off?
Why Does the Transaction Log Keep Growing or Run Out of Space?
How to shrink the SQL Server log
How do checkpoints work

How to rollback SQL update command?

In SQL server 2008 R2. Is it possible to do a rollback on a single update command?
I know there are other questiones like this on SO but i havent seen one specific for 2008 R2 and hence I may get the same answer, if that is the case then we can close this thread.
I did the a simple update without any transactions commands:
UPDATE myTable SET col1=somevalue WHERE....
Of course you can use explicit transactions such as
BEGIN TRAN
UPDATE ...
ROLLBACK
but I don't think you are asking about that?
If you have the option SET IMPLICIT_TRANSACTIONS ON then the command will not be committed or rolled back until you do so explicitly but this is not the default behaviour.
By default transactions are auto committed so when the command finishes successfully the results of the update will be committed. If the update was to encounter an error - including the connection being killed mid update it would auto rollback.
If your database is in full recovery mode you might want to try reading transaction log, finding which rows were affected and then reverting the update.
However, this is not supported by default, because MS stored transaction log in its own format that is not well documented.
Solution is to use commands such as DBCC LOG or fn_log or third party tool such as ApexSQL Log which does all of this automatically but comes with a price.
If you need more details, here are couple of posts on reading transaction log:
Read the log file (*.LDF) in sql server 2008
SQL Server Transaction Log Explorer/Analyzer

Is it possible to see what queries have been run in an uncommitted transaction?

My application (C++ using SQL Native Client with SQL Server 2000) is consistently finding its way into a hanging state. I believe this is because a transaction is left uncommitted someplace on a table and a SELECT query on that table, as a result of the open transaction, is blocking.
Unfortunately, I'm really having trouble determining where the hanging transaction might be in my code. Is there any way to get SQL Server to indicate what queries have been run on an uncommitted transaction?
if you have admin (sa) proviliges, you can run sp_Who, or sp_Who2
to show all server activity, by Spid, Run
Exec sp_Who2 [SpidNumber]
to just see the one session you are interested in...
To directly see open transactions, run
DBCC OPENTRAN (T-SQL)
Displays information about the oldest active transaction and the oldest distributed and nondistributed replicated transactions, if any, within the specified database. Results are displayed only if there is an active transaction or if the database contains replication information. An informational message is displayed if there are no active transactions.
Syntax
DBCC OPENTRAN
( {'database_name' | database_id}
) [ WITH TABLERESULTS [, NO_INFOMSGS]
]
Sql Server should, however, automatically rollback any open transaction when a user session is terminated.

Resources