Why are you excusing yourself?I just wrotequote: Originally posted by Peso So what are you asking for, really?You write you can't get rid of CURSOR so you are stuck with it.
here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127577&whichpage=2What you CAN do for possibly improving the speed of your procedure with the CURSOR, is this attempt below.The basics are that you fetch all your records including joins in one step, store the result in a temp-table and THEN apply the CURSOR over the temp table....DECLARE @x INTCREATE TABLE #Temp (st_code {datatype here}, st_desc {datatype here}, total_rm {datatype here}, total_cn {datatype here}, qty {datatype here})insert #tempSELECT m.st_code, m.st_desc, th.total_rm*th.forex_rate as total_rm, th.total_cn*th.forex_rate as total_cn, th.qtt_out - th.quantity as qtyFROM st_mast minner join(SELECT st_code, t.trx_type, t.qtt_out, t.quantity, t.total_price, CASE WHEN h.forex_rate = 0 then 1 else h.forex_rate END as forex_rate,CASE WHEN t.trx_type IN('CN','CNC','GRO') then 0 else t.total_price END as total_rm,CASE WHEN t.trx_type IN('CN','CNC','GRO') then t.total_price else 0 END as total_cnFROM st_trx t right outer join (SELECT CASE WHEN trx_type in('DO','CDO','DOL') then do_noWHEN trx_type in('INV','CS','CN','DN','POS','INC','CNC') then in_no END as ref_no,CASE WHEN trx_type in('DO','CDO','DOL') then (CASE WHEN in_date IS NULL or in_date='' then do_date else in_date END)WHEN trx_type in('INV','CS','CN','DN','POS','INC','CNC') then in_date END as date,forex_rate,trx_typeFROM st_head)h on t.trnx_ref = h.ref_no and t.trx_type=h.trx_typeWHERE date>='2007-01-01 00:00:00.000' and date<='2007-12-31 00:00:00.000')thon m.st_code=th.st_codeWHERE m.st_code>='' and m.st_code<='zzzzzzzzz'DECLARE STK_TRX_CURSOR CURSOR FORSELECT st_code, st_desc, total_rm, total_cn, qtyFROM #TempORDER BY st_codeBEGIN...WHILE @@FETCH_STATUS = 0BEGIN ...ENDFETCH NEXT FROM STK_TRX_CURSORINTO @st_code, @st_desc, @tov_total, @cn_total, @qty_totalENDCLOSE STK_TRX_CURSORDEALLOCATE STK_TRX_CURSORPRINT CAST('Grand total of report' as char(50))+'TOV'+CAST('' as char(100))+CAST(CONVERT(DECIMAL(14,2),@tov_Gtotal) as char(14))PRINT CAST('' as char(5))+CAST('' as char(45))+'CNV'+CAST('' as char(100))+CAST(CONVERT(DECIMAL(14,2),@cn_Gtotal) as char(14))PRINT CAST('' as char(5))+CAST('' as char(45))+'QTY'+CAST('' as char(100))+CAST(CONVERT(DECIMAL(14,2),@qty_Gtotal) as char(14)) E 12°55'05.63"N 56°04'39.26" |