Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi when to use transactions in sql server and please give a sample of it while inserting, updating and deleting records, i have told by my friend they are using transaction concept even while inserting data to database
Posted
Comments
Richard MacCutchan 18-Mar-13 8:21am    
A bit of Google searching will find you many samples.

sql transaction set is used when criteria like below,
I have two tables
PurchaseMain (BillNo,BillDate,BillTotalAmt)
PurchaseDetails (BillNo(FK),ProductId,ProductQty,ProductRate,ProductPrice)

you can understand above structure,
it is obvious while inserting data if any product from list is not saved then you need to rollback whole Bill. (Rollback discard saved data which is logically incomplete - here without products no mean to keep entry in purchase main table.)

in this case transaction se can be used
e.g.
begin transaction tran_purchase
--entry in purchase main
--for loop to add products in purchase details
    --using purchase main new bill no entry in Purchasedetail
    --if error then RollBack transaction tran_purchase and return  ...means error occur then rollback changes and terminate execution no need to execute next line.
--end of for loop
commit transaction tran_purchase  ... means save transaction no error found


some times it's used for consistency with locking concepts
e.g.
Vehicle number allocation
vehicle no. are alphaNumeric it's not int so that we have to manually produce it
at a time from different city branches in a state multiple command will given to database for generate vehicle no
so, chances of duplicate nos generation for single vehicle
this criteria suppose vehicle no is GJ-1-ZZ-9789
begin transaction tranVehiclenoGeneration with Lock level
--trim 4 digit from rigthside 
--increase no by 1
--check in database GJ-1-ZZ-9790 new no exist if no then go ahead  ... same way for middle 'zz' need to generate next series so, it's dependancy at that time no oher record should be added in table else it can make calculaions wrong if procedure is meanwhile
--so this case we need to keep logic in transaction set with suitable lock concept
--at end commit/rollback transaction will realease lock

Happy Coding!
:)
 
Share this answer
 
 
Share this answer
 
v2
Transactions are to be used to ensure that the database is always in a consistent state.
Use Transaction when you need to changes in database completely done.and if error occurred all changes Rollbacked to its previous state.
you can also read this blogpost
http://blogs.msdn.com/b/florinlazar/archive/2005/10/04/476775.aspx[^]
 
Share this answer
 
In a block of statement,Transactions are needed when all statement must done completely on not done any of them .

for example we have:
---------------
insert pramotion detail for a employee on tblpramotion
---------------
--- some calculations -----------
-- this is multiple statement block
-----------------
update salary for that person on tblsalary
---------------

by default sqlserver execute statement in autocommit mode.that is every single statement is executed and commited automatically.
Without using transaction if any error occurs on ----some calculation---- part of code then data is inserted and commited on tblpramotion but
salary is not updated for that employee.we get unexpected result on that condition .
on Such condition that all operation above must be completed or non of them then transaction needed.

Block will be :

BEGIN TRANSACTION
insert pramotion detail for a employee on tblpramotion
---------------
--- some calculations -----------
---this is multiple statement block
----------------- u
update salary for that person on tblsalary

COMMIT
 
Share this answer
 
v2
Comments
surendranew 19-Mar-13 8:23am    
Can u give an example while inserting data

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900