following is the exact code of the trigger i m using USE [Acme_Inv_Sys]GO/****** Object: Trigger [dbo].[Tr_UpdateItemCostledger_AI] Script Date: 02/06/2010 14:54:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[Tr_UpdateItemCostledger_AI] ON [dbo].[tb_item_cost_ledger] After insert AS--Start Global Declarationdeclare @mVDate datetime , @mItemId numeric(18,0)--End Global Declarationselect @mVDate = isnull(Vdate , '1900-01-01') from insertedupdate Tb_Item_Cost_Ledger set Total = isnull(debit, 0) - isnull(credit , 0) , TotalQty = isnull(DqtyIn, 0) - isnull(DqtyOut , 0) where autoid in (select autoid from inserted)---Create Temporary Table For Total If Not Avaialable IF OBJECT_ID('dbo.Tmp_AI_Item_Cost_Ledger_Item', 'U') IS NULLbegin CREATE TABLE [dbo].[Tmp_AI_Item_Cost_Ledger_Item]( Id numeric(18 , 0) identity(1, 1), AutoId numeric(18 , 0), DQtyIn numeric( 25 , 7) , DQtyOut numeric( 25 , 7) , TotalQty numeric( 25 , 7) , Stock numeric( 25 , 7) , Debit numeric( 25 , 7) , Credit numeric( 25 , 7) , Total numeric( 25 , 7) , Balance numeric( 25 , 7), average_cost numeric( 25 , 7) , VMID numeric(18 , 0), VDID numeric(18 , 0), Vtype numeric(18 , 0) PRIMARY KEY CLUSTERED ([Id] ASC))end ---End Create Temporary Table If Not Avaialable declare @RunningBalance numeric(25 , 7) , @RunningStock numeric(25 , 7) , @Average_Cost numeric(25 , 7) -----Start Cursor Of Itemdeclare items_All_Cur cursor for select ItemId from Inserted group by Itemidopen items_All_Cur fetch next from items_All_Cur into @mItemIdwhile @@fetch_status = 0 begin --Delete Any Previous Entry If Available In Temporary Table delete from Tmp_AI_Item_Cost_Ledger_Item set @RunningBalance = 0 set @RunningStock = 0 set @Average_Cost = 0 select @RunningStock = isnull(sum(DQtyIn) - sum(DQtyOut), 0) , @RunningBalance = isnull(sum(debit) - sum(credit), 0) from Tb_Item_Cost_Ledger where vdate < @mVDate and ItemId = @mItemId SELECT TOP 1 @Average_Cost = ISNULL(AVERAGE_COST, 0) from Tb_Item_Cost_Ledger where vdate < @mVDate and ItemId = @mItemId ORDER BY vdate DESC, VTYPE DESC, VMID DESC, OtherId DESC, Autoid DESC INSERT INTO Tmp_AI_Item_Cost_Ledger_Item SELECT Autoid, DqtyIn , DqtyOut , TotalQty , Null , Debit , Credit , Total , Null , Null , VMID , VDID , VType FROM Tb_Item_Cost_Ledger where Itemid = @mItemId and vdate > = @mvdate ORDER BY vdate , VTYPE , VMID , OtherId , Autoid --Get Running Total UPDATE Tmp_AI_Item_Cost_Ledger_Item set @RunningBalance = Tmp_AI_Item_Cost_Ledger_Item.Balance = case when a.vtype not in(dbo.fnc_sale_invoice_id(), dbo.fnc_Internal_transfer_note_id() ) then @RunningBalance + Tmp_AI_Item_Cost_Ledger_Item.Total when a.vtype in(dbo.fnc_sale_invoice_id(), dbo.fnc_Internal_transfer_note_id() ) and a.DqtyOut> 0 then @RunningBalance - (@Average_Cost * Tmp_AI_Item_Cost_Ledger_Item.DqtyOut) when a.vtype in(dbo.fnc_sale_invoice_id(), dbo.fnc_Internal_transfer_note_id() ) and a.DqtyIn > 0 then @RunningBalance + (@Average_Cost * Tmp_AI_Item_Cost_Ledger_Item.DqtyIN) end, @RunningStock = Tmp_AI_Item_Cost_Ledger_Item.Stock = @RunningStock + Tmp_AI_Item_Cost_Ledger_Item.TotalQty ,@Average_Cost = average_cost = case when (NULLIF(IsNull(@RunningBalance , 0), 0) / NULLIF(isnull(@RunningStock , 0), 0)) is null then @Average_Cost else (NULLIF(IsNull(@RunningBalance , 0), 0) / NULLIF(isnull(@RunningStock , 0), 0)) end FROM Tmp_AI_Item_Cost_Ledger_Item inner join Tmp_AI_Item_Cost_Ledger_Item a ON Tmp_AI_Item_Cost_Ledger_Item.id = a.id --Updating Item Cost ledger for balances UPDATE Tb_Item_Cost_Ledger SET Stock = isnull(Tmp_AI_Item_Cost_Ledger_Item.Stock , 0), Balance = isnull(Tmp_AI_Item_Cost_Ledger_Item.Balance , 0), Average_Cost = isnull(Tmp_AI_Item_Cost_Ledger_Item.Average_Cost , 0) ,Credit = (case when Tb_Item_Cost_Ledger.vtype in(dbo.fnc_sale_invoice_id(), dbo.fnc_Internal_transfer_note_id()) and Tb_Item_Cost_Ledger.DQtyOut > 0 then (isnull(Tmp_AI_Item_Cost_Ledger_Item.Average_Cost , 0) * isnull(Tb_Item_Cost_Ledger.DQtyOut, 0) ) else isnull(Tb_Item_Cost_Ledger.Credit, 0) end ) ,Debit = (case when Tb_Item_Cost_Ledger.vtype in(dbo.fnc_Internal_transfer_note_id()) and Tb_Item_Cost_Ledger.DQtyIn > 0 then isnull(Tmp_AI_Item_Cost_Ledger_Item.Average_Cost , 0) * isnull(Tb_Item_Cost_Ledger.DQtyIn, 0) else isnull(Tb_Item_Cost_Ledger.Debit, 0) end ) ,Total = (case when Tb_Item_Cost_Ledger.vtype in(dbo.fnc_sale_invoice_id(), dbo.fnc_Internal_transfer_note_id()) then (isnull(Tmp_AI_Item_Cost_Ledger_Item.Average_Cost , 0) * isnull(Tb_Item_Cost_Ledger.TotalQty, 0)) else isnull(Tb_Item_Cost_Ledger.Total, 0) end ) FROM Tmp_AI_Item_Cost_Ledger_Item inner join Tb_Item_Cost_Ledger ON Tmp_AI_Item_Cost_Ledger_Item.Autoid = Tb_Item_Cost_Ledger.Autoid-----Final Stock Balancedeclare @mStock numeric(18,0), @mAverage_Cost numeric(25,7)IF OBJECT_ID('dbo.Tmp_Item_Calculations', 'U') IS NULLbeginCreate Table Tmp_Item_Calculations( ItemId numeric (18,0), Stock numeric(25,7), Average_cost numeric(25,7) )endDelete from Tmp_Item_Calculations where itemid = @mItemIdset @mStock = 0 set @mAverage_Cost = 0 select Top 1 @mStock = isnull(Stock , 0) , @mAverage_Cost = isnull(Average_Cost, 0) From Tb_item_cost_ledger where itemid = @mItemId ORDER BY Vdate desc, VTYPE desc, VMID desc, OtherId desc, Autoid descINSERT INTO Tmp_Item_Calculations([ItemId], [Stock], [Average_cost]) VALUES ( @mItemId , @mStock , @mAverage_Cost) fetch next from items_All_Cur into @mItemIdendclose items_All_Curdeallocate items_All_CurDrop Table Tmp_AI_Item_Cost_Ledger_Item-----End Cursor Of Item---Create Temporary Table If Not Avaialable IF OBJECT_ID('dbo.Tmp_AI_Item_Cost_Ledger_LocItem', 'U') IS NULLbegin Create TABLE Tmp_AI_Item_Cost_Ledger_LocItem( Id numeric(18 , 0) identity (1, 1) , AutoId numeric(18 , 0), Total numeric( 25 , 7) , RunningTotal numeric( 25 , 7) PRIMARY KEY CLUSTERED ([Id] ASC))end ---End Create Temporary Table If Not Avaialable declare @mlocationId numeric(18,0), @mLocStock numeric(25,7), @RunningLocStock numeric(25 , 7) set @mItemId = 0 set @mlocationId = 0 -----Start Cursor Of Location Itemdeclare items_Loc_Cur cursor for select ItemId , locationid from Inserted group by Itemid , locationid open items_Loc_Cur fetch next from items_Loc_Cur into @mItemId, @mlocationIdwhile @@fetch_status = 0 begin --For Location Wise Stock delete from Tmp_AI_Item_Cost_Ledger_LocItem set @RunningLocStock = 0 select @RunningLocStock = isnull(sum(DQtyIn) - sum(DQtyOut), 0) from Tb_Item_Cost_Ledger where vdate < @mVDate and ItemId = @mItemId and locationid = @mlocationId --insert in temporary table INSERT INTO Tmp_AI_Item_Cost_Ledger_LocItem SELECT Autoid, TotalQty , null FROM Tb_Item_Cost_Ledger where Itemid = @mItemId and vdate > = @mvdate and locationid = @mlocationId ORDER BY vdate , VTYPE , VMID , OtherId , Autoid --Get Running Total UPDATE Tmp_AI_Item_Cost_Ledger_LocItem SET @RunningLocStock = Tmp_AI_Item_Cost_Ledger_LocItem.RunningTotal = @RunningLocStock + Tmp_AI_Item_Cost_Ledger_LocItem.total FROM Tmp_AI_Item_Cost_Ledger_LocItem inner join Tmp_AI_Item_Cost_Ledger_LocItem a ON Tmp_AI_Item_Cost_Ledger_LocItem.id = a.id --Updating Item Cost ledger for balances UPDATE Tb_Item_Cost_Ledger SET locStock = isnull(Tmp_AI_Item_Cost_Ledger_LocItem.RunningTotal , 0) FROM Tmp_AI_Item_Cost_Ledger_LocItem inner join Tb_Item_Cost_Ledger ON Tmp_AI_Item_Cost_Ledger_LocItem.Autoid = Tb_Item_Cost_Ledger.Autoid-----Start Final Location Wise Stock BalanceIF OBJECT_ID('dbo.Tmp_Item_Loc_Calculations', 'U') IS NULLbeginCreate Table Tmp_Item_Loc_Calculations( ItemId numeric (18,0), LocationId numeric (18,0), LocStock numeric(25,7) )endDelete from Tmp_Item_Loc_Calculations where itemid = @mItemId and LocationId = @mlocationIdset @mLocStock = 0 select Top 1 @mLocStock = isnull(LocStock , 0) From Tb_item_cost_ledger where itemid = @mItemId and locationid = @mlocationId ORDER BY Vdate desc, VTYPE desc, VMID desc, OtherId desc, Autoid descINSERT INTO Tmp_Item_Loc_Calculations([ItemId], [LocationId], [LocStock]) VALUES ( @mItemId , @mlocationId, @mLocStock )-----End Final Location Wise Stock Balance fetch next from items_Loc_Cur into @mItemId, @mlocationIdendclose items_Loc_Curdeallocate items_Loc_Curdrop table Tmp_AI_Item_Cost_Ledger_LocItem