|
rk68 (Programmer) |
4 Jun 12 7:49 |
Hi, I have 2 tables. I has Store Code & date of last transactions uploaded. The 2nd table has store code, date of transaction to be loaded & a tag whether its uploaded. I have written a procedure where in I check the 1st table to get the store code & the last date of transction + 1 day. If the above condition is met I update the 2nd table (tag field) with Y & have to continue for the next date of same store. If not available check the next store & the last updated date + one day from 1st table and repeat the process. Here the procedure...pl help where I am going wrong as this updates only 1 record. I m new to MS SQL. ================== Create PROCEDURE [dbo].[DPIL_TRN_Upload2] AS Begin declare @Loc_Cd1 VARCHAR(8) ; declare @Loc_Cd2 VARCHAR(8) ; declare @Odt1 datetime; declare @Odt2 datetime; declare @X_updt varchar(1) ; declare @cursor1_status int; declare @cursor2_status int; DECLARE cur1 CURSOR FOR SELECT Location_code,ODT_Max +1 FROM [cartesian].[dbo].[test_c_summ] order by location_code, odt_max; Delete from [Dpil].[dbo].[dpil_trn_summ]; Insert into [dpil].[dbo].[dpil_trn_summ] (location_code, order_date,t_upload) select distinct location_code, order_date, 'N' from [dpil].[dbo].[orders] where location_code in ('DDI71002','DPI65767') order by location_code, order_date; DECLARE cur2 CURSOR FOR SELECT location_code,order_date,t_upload FROM [dpil].[dbo].[dpil_trn_summ] order by location_code, order_date; OPEN cur1; OPEN cur2; fetch next from cur1 into @Loc_Cd1, @Odt1 select @cursor1_status = @@fetch_status while @cursor1_status = 0
begin fetch next from cur2 into @Loc_Cd2, @Odt2, @X_updt select @cursor2_status = @@fetch_status BEGIN print 'before '+@Loc_Cd1+ ' date' + convert(varchar(10),@Odt1) print 'before1 '+@Loc_Cd2 + ' date' + convert(Varchar(10),@Odt2);
while @cursor2_status = 0 BEGIN if @Loc_Cd1 = @Loc_Cd2 and @Odt1 = @Odt2 AND @X_updt='N' BEGIN print 'l1 '+@Loc_Cd1; print @Odt1; print 'l2 '+@Loc_Cd2; PRINT @ODT2;
update [dpil].[dbo].[dpil_trn_summ] set t_upload='Y' where location_code=@loc_cd1 and order_date=@Odt2 ; update [cartesian].[dbo].[test_c_summ] set ODT_Max = @Odt2 where location_code=@loc_cd1 ;
fetch next from cur2 into @Loc_Cd2, @Odt2, @X_updt select @cursor2_status = @@fetch_status End; Else print 'xx1 '+@Loc_Cd1; print @Odt1; fetch next from cur2 into @Loc_Cd2, @Odt2, @X_updt select @cursor2_status = @@fetch_status END; fetch next from cur2 into @Loc_Cd2, @Odt2, @X_updt select @cursor2_status = @@fetch_status END;
END; fetch next from cur1 into @Loc_Cd1, @Odt1 select @cursor1_status = @@fetch_status CLOSE cur2;
CLOSE cur1; END
=========== Thanks Raj |
|