You are on page 1of 3

create table #temp ( [tran_no] [dbo].[cmn_tranno] NULL, [tran_date] [datetime] NULL, GRN_NO VARCHAR(20) NULL, [vendor_code] [dbo].

[cmn_cust_ven_code] NULL, [vendor_name] [dbo].[CMN_NAME] NULL, stock_no varchar(20) null, variant varchar(20) null, qy numeric(18,8) null, item_name varchar(60) null, [item_value] [dbo].[cmn_Pamt] NULL, [gta] [dbo].[cmn_Pamt] NULL, [gta2] [dbo].[cmn_Pamt] NULL, [gta1] [dbo].[cmn_Pamt] NULL, [serv] [dbo].[cmn_Pamt] NULL, [servcess] [dbo].[cmn_Pamt] NULL, [servhcess] [dbo].[cmn_Pamt] NULL, [round] [dbo].[cmn_Pamt] NULL, [total] [dbo].[cmn_Pamt] NULL) insert into #temp ( [tran_no],-- [dbo].[cmn_tranno] NULL, [tran_date],-- [datetime] NULL, GRN_NO ,--VARCHAR(20) NULL, [vendor_code],-- [dbo].[cmn_cust_ven_code] NULL, [vendor_name],-- [dbo].[CMN_NAME] NULL, stock_no,-- varchar(20) null, variant,-varchar(20) null, qy, item_name,-varchar(60) null, [item_value],-- [dbo].[cmn_Pamt] NULL, [gta],-- [dbo].[cmn_Pamt] NULL, [gta2],-- [dbo].[cmn_Pamt] NULL, [gta1],-- [dbo].[cmn_Pamt] NULL, [serv],-- [dbo].[cmn_Pamt] NULL, [servcess],-- [dbo].[cmn_Pamt] NULL, [servhcess],-- [dbo].[cmn_Pamt] NULL, [round],-- [dbo].[cmn_Pamt] NULL, [total]-- [dbo].[cmn_Pamt] NULL) ) select distinct fs_tran_no, fs_tran_date, '', '', '', '',--a.stock_no, '',--a.stock_variant, 0, '',--a.stock_desc, 0,fs_post_amt,0,0,0,0,0,0,0 from fms..fs_postings where fs_tran_date between '2012-03-01 00:00:00.000' and '2012-03-31 00:00:00.000' and fs_account_no = 'L-03-02-05-01' AND FS_DR_CR_CODE = 'CR'

declare @tran_no [dbo].[cmn_tranno] DECLARE tran_no_cursor CURSOR FOR SELECT distinct tran_no FROM #temp ORDER BY tran_no OPEN tran_no_cursor FETCH tran_no_cursor INTO @tran_no while @@fetch_status = 0 begin update #temp set item_value = (select sum(item_value) from APDB..ap_invoice_detail where tran_no = @tran_no and ACCOUNT_NO NOT LIKE 'A-03-0405-01') where tran_no = @tran_no update #temp set VENDOR_CODE = (select VENDOR_CODE from APDB..ap_invoice_HEADER where tran_no = @tran_no ) where tran_no = @tran_no update #temp set gta2 = (select item_value from APDB..ap_invoice_detail where tran_no = @tran_no and stock_no ='GTA2' ) where tran_no = @tran_no update #temp set gta1 = (select item_value from APDB..ap_invoice_detail where tran_no = @tran_no and stock_no ='GTA1' ) where tran_no = @tran_no update #temp set serv = (select TRAN_STATUS from APDB..ap_invoice_HEADER where tran_no = @tran_no ) where tran_no = @tran_no UPDATE #TEMP SET GRN_NO = (SELECT TOP 1 GI_NUMBER FROM APDB..AP_INVOICE_DETAIL WHERE TRAN_NO = @tran_no AND ACCOUNT_NO = 'L-03-0102-09') where tran_no = @tran_no

FETCH tran_no_cursor INTO @tran_no END CLOSE tran_no_cursor DEALLOCATE tran_no_cursor

UPDATE #TEMP SET STOCK_NO =( SELECT DISTINCT STOCK_NO FROM POMDB..pur_gi_detail WHERE GI_NO =#TEMP.GRN_NO AND STOCK_NO LIKE 'RM%') UPDATE #TEMP SET qy =( SELECT SUM(moved_qty_puom) FROM POMDB..pur_gi_detail WHERE GI_NO =#TEMP.GRN_NO ) update #temp set item_name = (select distinct adjust_code from pomdb..pur_gi_adjust where GI_NO =#TEMP.GRN_NO AND adjust_code like 'FR%' ) select * from #temp drop table #temp --select * from fms..fs_postings where fs_tran_no = 'TRINV/007745/10-11' --select * from pomdb..pur_gi_adjust