SQL Delete with Transaction -


i have table has id, name, age. (table name : employee)

the id primary key in table. there sproc deletes entry given name. in sproc deletion, first select id based on name. if name valid delete.

declare @id uniqueidentifier begin transaction     select top 1 @id=id employee name=@name     if @@rowcount = 0     begin         rollback transaction         return error_not_found     end                      delete employee id = @id                 if @@rowcount = 0     begin         rollback transaction         return error_not_found     end commit transaction 

my question whether need need transaction in case or not. understand need transaction when want atomic operations (set of operations should pass/fail together).

please comment above scenario whether transaction required.. , pros / cons of / without transaction.

as answer question - if there no employee name neither delete nor select not change in database, rolling not necessary.

now if id unique , name not - selecting employee name pretty dangerous since have no real control employee (from ones same name) going delete. looks procedure should take id parameter, rather selecting name.


Comments