sql - Cursor while loop never stop. what is wrong in this code? -
/*create table m_b (code varchar(10),itemcount int,type varchar(30),amount money) insert m_b values ('b001',1,'dell',10) insert m_b values ('b001',1,'dell',10) insert m_b values ('b001',1,'apple',10) insert m_b values ('b001',2,'apple',20) insert m_b values ('b001',2,'apple',20) insert m_b values ('b114',1,'apple',30.5) insert m_b values ('b114',1,'apple',10) */ --select * #temp m_c 1=2 declare cur_test cursor select jobid,start,end_date,dayrate m_c go declare @jobid int declare @start date,@end_date date declare @dayrate int open cur_test fetch cur_test @jobid,@start,@end_date,@dayrate --begin declare @jan int set @jan=0 while (@@sqlstatus != 2) begin if month(@start)=1 begin select @jan= @jan + datediff(dd,'2013-01-31',@start) -- testing purpose insert #temp values (@jan,@start,@end_date,56) end if month(@end_date)=1 begin select @jan= @jan + datediff(dd,'2013-01-31',@end_date) -- testing purpose insert #temp values (@jan,@start,@end_date,57) end fetch cur_test @jobid,@start,@end_date,@dayrate end select @jan waitfor delay '00:00:10' close cur_test deallocate cursor cur_test --end select * #temp --truncate table #temp
i'm not sure why never stop, use below condition
while @@sqlstatus = 0
so try use instead of
while (@@sqlstatus != 2)
Comments
Post a Comment