Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Transaction Handling...

Author  Topic 

anandc
Starting Member

20 Posts

Posted - 2005-12-30 : 08:12:06
I am using a table variable and doing multiple updates on it in a procedure, is it required to put all the update statements in a transaction (BEGIN TRANSACTION...COMMIT TRANSACTION) block. Below is the small part of my code

DECLARE @tblCalcClearingCharge TABLE
(Row_Id int IDENTITY(1,1),
Remit_ID int,
Trans_ID int,
Rep_ID int,
Trans_Src varchar(3),
CUSIP_ID varchar(9),
Prod_Type varchar(3),
Prod_Sub_Type varchar(3),
Agency_Principal_Ind char(1),
Order_System varchar(3),
Employee_Trade_Ind varchar(3),
Clr_Chrg decimal(14,2),
Units_Traded numeric(21,2),
GDC decimal(16,2),
ClrChrg_Id int,
Agency_Fee decimal(14,2),
Principal_Fee decimal(14,2),
Order_Markup decimal(14,2),
Share_Markup decimal(14,2),
OrderSystem_Markup decimal(14,2),
Employee_Fee decimal(14,2),
Min_ClrChrg decimal(14,2),
Calculated_ClrChrg decimal(14,2),
Adj_ClrChrg decimal(14,2)
)

--add employee fee for an employee trade
UPDATE @tblCalcClearingCharge
SET Calculated_ClrChrg = Calculated_ClrChrg + Employee_Fee
WHERE Employee_Trade_Ind = 'Y' AND ISNULL(ClrChrg_Id, 0) <> -1

--add agency fee for agency trade
UPDATE @tblCalcClearingCharge
SET Calculated_ClrChrg = Calculated_ClrChrg + Agency_Fee
WHERE Employee_Trade_Ind = 'N' AND
Agency_Principal_Ind = 'A' AND ISNULL(ClrChrg_Id, 0) <> -1

--add ordersystem markup if order system indicator is '1'
UPDATE @tblCalcClearingCharge
SET Calculated_ClrChrg = Calculated_ClrChrg + OrderSystem_Markup,
Adj_ClrChrg = OrderSystem_Markup
WHERE Order_System = '1' AND ISNULL(ClrChrg_Id, 0) <> -1


- Anand

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-12-30 : 09:17:26
>>is it required to put all the update statements in a transaction
Probably not. Depends on if you want an "all or nothing" thing happening.

<general explanation>
Explicit transaction handling (begin tran / [rollback | commit] tran) is for when you want to combine a series of statements into a single transaction. (all commit or none)
if you don't use explicit transactions then sql server will commit each statement individually with an implicit transaction so you don't have to.
</general explanation>

EDIT:
Ok, now I've actually read your update statments and see that that could qualify for an "All or None" scenario. You would typically be checking for violation of business rules either befor or after the update attempt and if an a violation occurs (or a sql error occurs), rollback the transaction. However, your statements are only changing table variables. So I would only employ transaction control on the statements that update the actual tables.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -