Tuesday, October 27, 2009

Transcation in Sql Query in SQL server

Many times it occurs that we are writing a sql query and executing it to check what is it affect to table.
for example Delete statement or update statement
We write a update / Delete statemet in Query bulider of Sql server enviroment and execute it
Suddenly we find it has Affected multiple records which should not be occured.

And then we suffer the data lost.

Instead while doing R&D on delete/update query in SQL query builder in sql server, start a new
transaction in sql server .

Example:
declare @EmpID int
set @EmpID = '47'
Begin Tran
DELETE FROM [Employee]
WHERE Employee_ID = @EmpID
--COMMIT TRAN
--ROLLBACK




and if it affects multiple records which we don't aspect to happen
just execute
ROLLBACK statement

Otherwise it we get expected result of update/delete Just Execute COMMIT TRAN

No comments:

Post a Comment