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.
| Author |
Topic |
|
ashokv
Starting Member
6 Posts |
Posted - 2006-11-13 : 00:59:18
|
| Hi allHere are my sprocProcedure 1CREATE procedure sp_Calculation_Invoice(@invoice_id bigint)ASSET ARITHABORT ONDECLARE @item_id bigintdeclare @freight numeric(15,4)declare @insurance numeric(15,4)DECLARE @rate_Freight decimal(15,4)DECLARE @rate_Insurance decimal(15,4)DECLARE @rate_PackChg decimal(15,4)DECLARE @fobDiscount decimal(15,4)DECLARE @otherDedcution decimal(15,4)DECLARE @addedChgs varchar(4)DECLARE @TotalItemValue decimal(15,5)DECLARE @FOBVALUE decimal(15,5)DECLARE @INVVALUE decimal(15,5)DECLARE @invcur VARCHAR(3)DECLARE @jobno bigintDECLARE @UnitinRs decimal (15,2)DECLARE @Rate decimal (15,4)DECLARE @ComisnRate NUMERIC(8,5)DECLARE @DiscntRate NUMERIC(8,5)DECLARE @OthDedRate NUMERIC(8,5)print @invoice_id--RETRIEVE TOTAL ITEM VALUE FOR THAT INVOICE ID SELECT @TotalItemValue= SUM(ISNULL(TOTAL_VAL_FC,0)) FROMS_ITEMS WHERE INVOICE_ID=@invoice_id--Convert to FC if insurance and freight are in INRselect@freight = case when isnull(FRGHT_CUR,'') = 'INR'then isnull(FRGHT_AMT,0.00)/isnull(EXCG_RATE,0.00)else isnull(FRGHT_AMT,0.00)end,@insurance = case when isnull(INSURC_CUR,'') = 'INR'then isnull(INSURC_AMT,0.00)/isnull(EXCG_RATE,0.00)else isnull(INSURC_AMT,0.00)end froms_invoice where invoice_id = @invoice_id--GET INVOICE DETAILS AND INSERT INTO TEMP INVOICE DETAILSSELECT@rate_Freight = (@freight/@TotalItemValue) * 100,@rate_PackChg = (ISNULL(PACK_CHARGE,0)/@TotalItemValue) * 100,@rate_Insurance = CASEWHEN ISNULL(INSURC_RATE,0.00)=0.00 THEN (@insurance/@TotalItemValue) * 100ELSE ISNULL(INSURC_RATE,0.00) END,@fobDiscount = CASEWHEN ISNULL(DISCNT_RATE,0.00)=0.00 THEN (ISNULL(DISCNT_AMT,0)/@TotalItemValue) * 100ELSE ISNULL(DISCNT_RATE,0.00) END,@otherDedcution = CASEWHEN ISNULL(OTH_DEDUCT_RATE,0.00)=0.00 THEN (ISNULL(OTH_DEDUCT_AMT,0)/@TotalItemValue) * 100ELSE ISNULL(OTH_DEDUCT_RATE,0.00) END,@addedChgs = ISNULL(ADDED_CHARGES,''),@invcur = ISNULL(INV_CUR,''),@jobno= JOB_NO FROMS_INVOICE WHERE INVOICE_ID=@invoice_id--select * from #TMPCALCULATE*/CREATE TABLE #TMPITEMAMT (ITEM_ID BIGINT,FOB_VALUE DECIMAL(15,5),INV_VALUE DECIMAL(15,5))--Open Cursor to select all items for the given invoice id/* print( '@rate_Freigh:' + convert(varchar,@rate_Freight ))print('@rate_Insurance:'+ convert(varchar,@rate_Insurance ))print( '@rate_PackChg:'+convert(varchar,@rate_PackChg ))print('@fobDiscount :'+ convert(varchar,@fobDiscount ))print( '@otherDedcution:'+ convert(varchar,@otherDedcution))print( ' @addedChgs:'+ convert(varchar,@addedChgs))*/--insert into #TMPITEMAMT SELECT ITEM_ID,null,null FROM S_ITEMS WHERE INVOICE_ID=@Invoice_idDECLARE @ItemCursor CURSOR SET @ItemCursor = CURSOR FAST_FORWARD FOR --SELECT ITEM_ID FROM #TMPITEMAMTSELECT ITEM_ID FROM S_ITEMS WHERE INVOICE_ID=@Invoice_idOPEN @ItemCursor FETCH NEXT FROM @ItemCursor INTO @item_id WHILE @@FETCH_STATUS = 0BEGIN-- insert into DebugLog values('ITEM - BEGIN','@item_id',@item_id)--loop through the cursor to execute stored procexec sp_Calculation_Items @item_id, @rate_Freight , @rate_Insurance , @rate_PackChg , @fobDiscount , @otherDedcution, @addedChgs-- insert into DebugLog values('ITEM - END','@item_id',@item_id)-- Clean up Accessory/ThirdParty/CESS table for this ITEMID--insert into DebugLog values('before cleanup','@item_id',@item_id)exec sp_CleanUpItem @item_id-- insert into DebugLog values('ITEM - CLEANED','@item_id',@item_id)FETCH NEXT FROM @ItemCursor INTO @item_id ENDinsert into DebugLog values('LAST ITEM COMPLETED','@item_id',@item_id)CLOSE @ItemCursor DEALLOCATE @ItemCursor--Retrieve Exchange details for that invoiceid currencycodeSELECT @UnitinRs = EXCG_RATE,@Rate = 1 -- ISNULL(RATE,0) --In the Invoice table the Exchg rate is always stored for 1 unit of currency. Hence this is hard coded as "1"FROMS_INVOICEWHEREINVOICE_ID=@Invoice_id --Update Item FOB Value in S_ITEMS /*SELECT @FOBVALUE=FOB_VALUE,@INVVALUE=INV_VALUE FROM #TMPITEMAMT WHERE ITEM_ID=@item_id*/--print('LOOK AT HERE11')UPDATE S_ITEMSSET FOB_VAL_FC = tmp.FOB_VALUE,FOB_VAL_RS = tmp.FOB_VALUE * (@UnitinRs / @Rate)FROM S_ITEMS i, TempItemAmt tmpWHERE i.ITEM_ID = tmp.item_id-- calculate FOB value and Invoice value from SUM(FOB_VAL) and SUM(INV_VALUE) from temp tableSELECT @FOBVALUE=SUM(ISNULL(FOB_VALUE,0)),@INVVALUE=SUM(ISNULL(INV_VALUE,0)) FROM TempItemAmt --WHERE ITEM_ID=@item_id-- Update the above two values in the S_INVOICE for that invoice idUPDATE S_INVOICESET FOB_FC = @FOBVALUE,INV_FC = @INVVALUE,FOB_RS = @FOBVALUE * (@UnitinRs / @Rate), INV_RS = @INVVALUE * (@UnitinRs / @Rate)WHEREINVOICE_ID=@Invoice_id-----------------------/*MODIFICATION BY MANI Date: 14-Oct-2006*/ SELECT @ComisnRate = ISNULL(comisn_rate, 0) FROM S_INVOICE WHERE INVOICE_ID=@Invoice_id and comisn_rate >0SELECT @DiscntRate = ISNULL(discnt_rate, 0) FROM S_INVOICE WHERE INVOICE_ID=@Invoice_id AND discnt_rate > 0SELECT @OthDedRate = ISNULL(oth_deduct_rate, 0) FROM S_INVOICE WHERE INVOICE_ID=@Invoice_id AND oth_deduct_rate > 0IF @ComisnRate > 0BEGINUPDATE S_INVOICESET COMISN_AMT = @TotalItemValue * (@ComisnRate/100)WHEREINVOICE_ID=@Invoice_idENDIF @DiscntRate > 0BEGINUPDATE S_INVOICESET DISCNT_AMT = @TotalItemValue * (@DiscntRate/100)WHEREINVOICE_ID=@Invoice_idENDIF @OthDedRate > 0BEGINUPDATE S_INVOICESET oth_deduct_amt = @TotalItemValue * (@OthDedRate/100) WHEREINVOICE_ID=@Invoice_idEND------------------------print('LOOK AT HERE')--select * from #TMPITEMAMTdrop table #TMPCALCULATEdrop table #TMPITEMAMTGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOProcedure 2CREATE procedure sp_Calculation_Items(@item_id bigint,@rate_Freight decimal(9,4),@rate_Insurance decimal(9,4),@rate_PackChg decimal(9,4),@fobDiscount decimal(9,4),@otherDedcution decimal(9,4),@addedChgs varchar(4) )ASSET ARITHABORT ONDECLARE @FOBVALUE decimal(15,5)DECLARE @INVVALUE decimal(15,5)DECLARE @AdjustmentVal decimal(15,5)DECLARE @TotalValue decimal(15,5)/*CREATE TABLE #TMPITEMAMT(ITEM_ID BIGINT,FOB_VALUE DECIMAL(9,2),INV_VALUE DECIMAL(9,2))*/------------------------------------------------------------------------------------/*SELECT@rate_Freight = isnull(rate_Freight,0.00),@rate_Insurance = isnull(rate_Insurance,0.00),@rate_PackChg = isnull(rate_PackChg,0.00),@fobDiscount = isnull(fobDiscount,0.00),@otherDedcution = isnull(otherDedcution,0.00),@addedChgs = isnull(@addedChgs,0.00)FROM#TMPCALCULATE */-----------------------------------------------------------------------------------SELECT @TotalValue=TOTAL_VAL_FCFROM S_ITEMSWHERE ITEM_ID=@item_id--@AdjustmentVal is common to all type of added charges and nature of contractSET @AdjustmentVal = ((@rate_PackChg/100)*@TotalValue) - ((@fobDiscount/100)*@TotalValue) - ((@otherDedcution/100)*@TotalValue)/*print ('@item_id')print( @item_id)print('1')print((@rate_PackChg/100)*@TotalValue) print('2')print((@fobDiscount/100)*@TotalValue) print('3')print((@otherDedcution/100)*@TotalValue)print('@TotalValue')print(@TotalValue)print('@rate_PackChg')print(@rate_PackChg)print('@fobDiscount')print(@fobDiscount)print('@otherDedcution')print(@otherDedcution)print('@AdjustmentVal')print(@AdjustmentVal)*/insert into debuglog values('Inside Items','@@error',@@error)insert into debuglog values('Inside Items -2 ','@addedChgs',@addedChgs)IF @addedChgs = 'F' --FreightBEGIN--FREIGHT IS ALREADY ADDED INTO THE TOTAL VALUE. SO SUBTRACT IT TO GET THE FOB VALUESET @FOBVALUE=@TotalValue + @AdjustmentVal - ((@rate_Freight/100)*@TotalValue)--FREIGHT IS ALREADY ADDED, HENCE ADD ONLY INSURANCE TO THE INVOICE VALUESET @INVVALUE=@TotalValue + @AdjustmentVal + ((@rate_Insurance/100)*@TotalValue) ENDIF @addedChgs = 'I' --Insurance BEGIN--INSURANCE IS ALREADY ADDED INTO THE TOTAL VALUE. SO SUBTRACT IT TO GET THE FOB VALUESET @FOBVALUE=@TotalValue + @AdjustmentVal - ((@rate_Insurance/100)*@TotalValue)--INSURANCE IS ALREADY ADDED, HENCE ADD ONLY FREIGHT TO THE INVOICE VALUESET @INVVALUE=@TotalValue + @AdjustmentVal + ((@rate_Freight/100)*@TotalValue) ENDIF @addedChgs = 'N' --NoneBEGIN--NONE ADDED INTO THE TOTAL VALUE. SO FOB VALUE EQUALS TOTAL VALUESET @FOBVALUE=@TotalValue + @AdjustmentVal--NONE ADDED INTO THE TOTAL VALUE. HENCE ADD BOTH FREIGHT & INSURANCE TO THE INVOICE VALUESET @INVVALUE=@TotalValue + @AdjustmentVal + ((@rate_Freight/100)*@TotalValue) + ((@rate_Insurance/100)*@TotalValue)ENDIF @addedChgs = 'B' --BothBEGIN--BOTH ADDED INTO THE TOTAL VALUE. SO SUBTRACT BOTH TO GET THE FOB VALUESET @FOBVALUE=@TotalValue + @AdjustmentVal - ((@rate_Freight/100)*@TotalValue) - ((@rate_Insurance/100)*@TotalValue)--BOTH ADDED INTO THE TOTAL VALUE. HENCE TOTAL VALUE EQUALS INVOICE VALUESET @INVVALUE=@TotalValue + @AdjustmentVal END/*INSERT INTO #tmpITEMAMTVALUES(@item_id,@FOBVALUE,@INVVALUE)*/insert into debuglog values('Inside Items -2 ','@@error',@@error)insert into TempItemAmt values(@item_id,@FOBVALUE,@INVVALUE)--update #tmpITEMAMT set FOBVALUE = @FOBVALUE,INVVALUE=@INVVALUE where item_id = @item_id--select * from #tmpITEMAMTGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOprocedure 3CREATE PROCEDURE sp_Calculation_SB(@job_id bigint)ASSET ARITHABORT ONdeclare @invoice_id bigintDECLARE @SBFOBFC DECIMAL(15,5)DECLARE @SBFOBRS DECIMAL(15,5) insert into DebugLog values('SB','JOB_NO',@job_id)--open cursor to select all invoices for the given job_idDECLARE @InvoiceCursor CURSOR SET @InvoiceCursor = CURSOR FAST_FORWARD FOR SELECT INVOICE_ID FROM S_INVOICE WHERE JOB_NO=@job_idprint @job_idOPEN @InvoiceCursor FETCH NEXT FROM @InvoiceCursor INTO @invoice_id WHILE @@FETCH_STATUS = 0BEGIN--loop through the cursor to execute store proc-- insert into DebugLog values('INVOICE','@invoice_id',@invoice_id)exec sp_Calculation_Invoice @invoice_idinsert into DebugLog values('ERROR1','@invoice_id',@@error)FETCH NEXT FROM @InvoiceCursor INTO @invoice_id ENDinsert into DebugLog values('ERROR2','@invoice_id',@@error)--close cursorCLOSE @InvoiceCursor DEALLOCATE @InvoiceCursorinsert into DebugLog values('ERROR3','@invoice_id',@@error)-- calculate FOB value and TOTAL Invoice value from SUM(FOB_VAL) and SUM(INV_VALUE) from S_INVOICESELECT @SBFOBFC = SUM(isnull(FOB_FC,0)), @SBFOBRS = SUM(isnull(FOB_RS,0)) FROM S_INVOICE WHERE JOB_NO = @job_id-- Update the above two values in the S_SB for that job idUPDATE S_SBSET FOB_RS = @SBFOBRS,FOB_FC = @SBFOBFCWHEREJOB_NO = @job_id--exec stored proc to calculate DBK amountinsert into DebugLog values('ERROR4','@invoice_id',@@error)exec sp_calculation_DBKMAIN @job_idinsert into DebugLog values('ERROR5','@invoice_id',@@error)--exec stored proc to calculate DEPB amountexec sp_calculation_DEPBMAIN @job_idGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOprocedure 4CREATE procedure sp_CleanUpItem(@itemid bigint) asSET ARITHABORT ONdeclare @accessory as char(1)declare @thirdparty as char(1)declare @cess as char(1)declare @DBK char(1)declare @DFIA char(1)declare @LICENSE char(1)declare @DEPB char(1)declare @EOU char(1)declare @JOBWORK char(1)declare @Jobno bigint insert into debuglog values('Cleanup-starts here',@itemid,@@error)select @Jobno=job_no from s_items where item_id=@itemidselect @accessory = ACCESSORIES,@thirdparty = THIRD_PARTY,@cess = CESS_ITEM from S_ITEMS where ITEM_ID=@itemid/*print ('@accessory=' + @accessory)print ('@thirdparty=' + @thirdparty)print ('@cess=' + @cess)*/if @accessory='N'delete from S_ITEM_ACCESS where ITEM_ID=@itemidif @thirdparty='N'delete from S_THIRD_PARTY where ITEM_ID=@itemidif @cess='N'delete from S_CESS where ITEM_ID=@itemidSELECT @DBK = SCM.DBK,@DEPB = SCM.DEPB,@DFIA = SCM.DFIA,@EOU = SCM.EOU,@LICENSE = LICENSE,@JOBWORK = JOBWORKFROM S_ITEMS IT, D_SCHEME_CODE_MAPPING SCMWHERE IT.SCHEME_CD = SCM.SCHEME_CODEAND IT.ITEM_ID = @itemidIf @DBK='N'DELETE FROM S_DBK WHERE ITEM_ID=@itemidIf @DEPB='N'DELETE FROM S_DEPB WHERE ITEM_ID=@itemidPRINT ('DFIA: ' + @DFIA)If @DFIA='N'DELETE FROM S_DFRC WHERE ITEM_ID=@itemid/*If @EOU='N'IF NOT EXISTS (SELECT 1 FROM S_ITEMS WHERE JOB_NO = @Jobno AND --SELECT EOU_SCH FROM S_ITEMS WHERE JOB_NO=26029DELETE FROM S_EOU_REP WHERE ITEM_ID=@itemid*/If @LICENSE='N'DELETE FROM S_DEEC WHERE ITEM_ID=@itemidIf @JOBWORK='N'DELETE FROM S_JOB_WORK WHERE ITEM_ID=@itemidinsert into debuglog values('Cleanup-ends here',@itemid,@@error)GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOprocedure 5CREATE procedure sp_calculation_DBK@item_id varchar(10)asSET ARITHABORT ONdeclare @maxEffDt smalldatetimedeclare @dbk_serno varchar(15)declare @DBK_RATE_ADV decimal(8,2)declare @DBK_RATE_SPE_NUMBER decimal(8,2)declare @DBK_RATE_EXCISE decimal(8,2)declare @DBK_RATE_CUSTOM decimal(8,2)declare @fob_val_rs decimal(15,5)select @dbk_serno = dbk_sernofrom s_dbk where item_id = @item_idSelect @maxEffDt= max(effect_date) from d_dbk_sched where dbk_serno = @dbk_sernoselect @DBK_RATE_ADV = DBK_RATE_ADV,@DBK_RATE_SPE_NUMBER = DBK_RATE_SPE_NUMBER,@DBK_RATE_EXCISE = DBK_RATE_EXCISE,@DBK_RATE_CUSTOM = DBK_RATE_CUSTOM from d_dbk_sched sdbkwhere sdbk.effect_date = @maxEffDtand dbk_serno = @dbk_sernoselect @fob_val_rs = isnull(fob_val_rs,0.00)from s_items where item_id = @item_idupdate s_dbkset DBK_RT_ADV =isnull( @DBK_RATE_ADV,0.00),DBK_RT_SP_NO = isnull(@DBK_RATE_SPE_NUMBER,0.00),DBK_RATE_EX = isnull(@DBK_RATE_EXCISE,0.00),DBK_RATE_CU = isnull(@DBK_RATE_CUSTOM,0.00),dbk_val =casewhen dbk_claim_type = 2 then (isnull(@fob_val_rs, 0) * (isnull(@dbk_rate_adv, 0)/100))when dbk_claim_type = 3 then (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))when dbk_claim_type = 5 OR dbk_claim_type = 6 OR dbk_claim_type = 8 then case when (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100)) > (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))then (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))else (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100))end else 0end,EXCISE_AMT =casewhen dbk_claim_type = 2 then 0when dbk_claim_type = 3 then (isnull(@dbk_rate_EXCISE, 0) * isnull(t.dbk_qty, 0))when dbk_claim_type = 5 OR dbk_claim_type = 6 OR dbk_claim_type = 8 then case when (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100)) > (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))then (@DBK_RATE_EXCISE * T.DBK_QTY)else (@FOB_VAL_RS* (@DBK_RATE_EXCISE/100))end else 0end,CUSTOM_AMT =casewhen dbk_claim_type = 2 then 0when dbk_claim_type = 3 then (isnull(@dbk_rate_custom, 0) * isnull(t.dbk_qty, 0))when dbk_claim_type = 5 OR dbk_claim_type = 6 OR dbk_claim_type = 8 then case when (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100)) > (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))then (@DBK_RATE_CUSTOM * T.DBK_QTY)else (@FOB_VAL_RS* (@DBK_RATE_CUSTOM/100))end else 0endfrom s_dbk twhere t.item_id=@item_iddeclare @dbkamt numeric(15,5)select@dbkamt =casewhen dbk_claim_type = 2 then (isnull(@fob_val_rs, 0) * (isnull(@dbk_rate_adv, 0)/100))when dbk_claim_type = 3 then (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))when dbk_claim_type = 5 OR dbk_claim_type = 6 OR dbk_claim_type = 8 then case when (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100)) > (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))then (isnull(@dbk_rate_spe_number, 0) * isnull(t.dbk_qty, 0))else (isnull(@fob_val_rs, 0) *(isnull(@dbk_rate_adv, 0)/100))end else 0endfrom s_dbk twhereitem_id = @item_idinsert into DebugLog values('DBK','dbk_val',@item_id)GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOCREATE procedure sp_calculation_DBKMAIN@job_no bigintasSET ARITHABORT ONdeclare @item_id varchar(15)create table #tempitemdbk(item_id varchar(25))insert into #tempitemdbkselect item_idfroms_dbkwherejob_no = @job_noDECLARE cur_itemdbk CURSOR FORselect * from #tempitemdbkopen cur_itemdbkfetch next from cur_itemdbk into@item_idwhile @@fetch_status=0beginprint (@item_id)exec sp_calculation_DBK @item_idfetch next from cur_itemdbk into@item_idendCLOSE cur_itemdbk DEALLOCATE cur_itemdbkdrop table #tempitemdbk/*SET DBK VALUES IN INVOICE AND SB TABLES */SELECT INV_SERNO, SUM(ISNULL(DBK_VAL , 0)) INV_DBKINTO #tmpINVDbkFROM S_DBKWHERE JOB_NO = @job_noGROUP BY INV_SERNOUPDATE S_INVOICESET DBK_RS = T.INV_DBKFROM S_INVOICE INV, #tmpINVDbk TWHERE INV.INV_SERNO = T.INV_SERNOAND INV.JOB_NO = @job_noDECLARE @DBK_SB DECIMAL(9,2)SELECT @DBK_SB = SUM(ISNULL(INV_DBK, 0))FROM #tmpINVDbkUPDATE S_SBSET DBK_RS = @DBK_SBFROM S_SB SBWHERE SB.JOB_NO =@job_noDROP TABLE #tmpINVDbkGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOprocedure 6CREATE procedure sp_calculation_DEPB@item_id varchar(10)asSET ARITHABORT ONdeclare @item_f_cur numeric(15,5)declare @item_qty numeric(15,5)select @item_f_cur=isnull(it.item_rate,0)*isnull(inv.excg_rate,0), @item_qty=isnull(it.quantity,0) from s_items it join s_invoice inv on it.invoice_id= inv.invoice_id where it.item_id=@item_idprint @item_f_curprint @item_qtyupdate s_depbset DEPB_AMT = casewhen m.depb_claim_type = 2 then (isnull(i.fob_val_rs,0) * (isnull(m.rate,0)/100))when m.depb_claim_type = 5 thencasewhen (@item_f_cur) < (isnull(m.value_rest,0))then (isnull(i.fob_val_rs,0) * (isnull(m.rate,0)/100)) else (isnull(m.value_rest,0) * isnull(t.quantity,0) * (isnull(m.rate,0)/100))-- else 0endelse 0endfrom s_items i, s_depb t, d_depb mwhere t.item_id = i.item_idand t.grp_cd = m.grp_cdand t.item_cd = m.item_cdand m.effect_date = (select max(sdepb.effect_date) from d_depb sdepb,s_depb twhere sdepb.grp_cd = t.grp_cd and sdepb.item_cd = t.item_cd and t.item_id = @item_id) and t.item_id=@item_idGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOCREATE procedure sp_calculation_DEPBMAIN@job_no bigintasSET ARITHABORT ONdeclare @item_id varchar(15)create table #tempitemdepb(item_id varchar(25))insert into #tempitemdepbselect item_idfroms_depbwherejob_no = @job_noDECLARE cur_itemdepb CURSOR FORselect * from #tempitemdepbopen cur_itemdepbfetch next from cur_itemdepb into@item_idwhile @@fetch_status=0beginprint (@item_id)exec sp_calculation_DEPB @item_idfetch next from cur_itemdepb into@item_idendCLOSE cur_itemdepb DEALLOCATE cur_itemdepbdrop table #tempitemdepbGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOThanks,Ashok VChennai, India |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-13 : 01:08:42
|
| You expect us to understand your 10-pages of code without single line of description and also come up with solution, all for free?You want us to do your work? Then you are wrong, buddy!Tell us exactly what the problem is and what steps you have taken so far to solve it. Also some sample data and DDLs are recommended.Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-13 : 01:11:28
|
| 10 pages? I had to scroll down 22 times...Peter LarssonHelsingborg, Sweden |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-11-13 : 01:51:01
|
| No idea what your problem is, but:Is that how you format your code? If you have a formatted version please post it here with [code] tags.Read up about:Why calling your procedures "sp_XXX" is a bad ideaWhy you should be prefixing your objects by the name of their ownerWhyinsert into debuglog values('Inside Items','@@error',@@error)(and others like it) is unlikely to store anything useful because of scopeI don't think 25, or so, comments is enough for a chunk of code that bigYou would do better to get the relevant data into #TEMP tables and use that to control the set that the SProcs must work on, rather than using you approach of Cursors which is much slower. The fact that there is only one JOIN in all this code suggests that you are probably not approaching the problem from a set-based perspective.Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-11-13 : 10:55:50
|
| ashok, can you explain what that sp does?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|