Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to use while loop in stored procedure.But when i use while loop in storedprocedure,data is not inserted.i have not any error.but it stop working.
how to use while loop in sp?
i have try this...

ALTER PROCEDURE dbo.InsertInOutTime


AS

Declare @InTime datetime
Declare @OutTime datetime
Declare @CheckTime datetime
Declare @id int
Declare @Userid int
Declare @StartTime datetime
Declare @EndTime datetime
Declare @Yesterday datetime
Declare @i int

select @i=-1
while(@i>-10) begin

select @Yesterday =DateAdd(dd,-@i,getdate())  --change : Chack it for not only yesterday but upto next come days ,use While loop for that.
select top 1 @id=id from Biometrics where Userid=@Userid and [Date]=@Yesterday order by id desc  


if(@InTime is not null and @OutTime is null)
begin
select @StartTime=StartTime,@EndTime=EndTime from AssignTimeTable where Userid=@Userid 
UPDATE       Biometrics
		SET                Present_Day='Half Day (AM)' ,Present='P' where id=@id  
end


if(@InTime is null and @OutTime is not null)
begin
select @StartTime=StartTime,@EndTime=EndTime from AssignTimeTable where Userid=@Userid 
	
	UPDATE       Biometrics
		SET                Present_Day='Half Day (PM)' ,Present='P' where id=@id  
end






select	@i=@i-1
end--end of while loop 
	--End of code.
Posted
Updated 9-Feb-13 1:00am
v3
Comments
Abhishek Pant 9-Feb-13 6:56am    
what is the problem?

1 solution

Well no. It won't exit, will it?
There is little point in you doing this:
SQL
select	@i=@i-1
At the bottom of your loop if right at the top you do this:
SQL
select top 1 @id=id from Biometrics where Userid=@Userid and [Date]=@Yesterday order by id desc
and you don't change any of the details in the WHILE clause! If it works once, it will set the same value each and every time!
 
Share this answer
 
Comments
Member 9511889 9-Feb-13 8:01am    
what you trying to say. this line i have to put at the end of loop,how it effect the code,i cant understand. select top 1 @id=id from Biometrics where Userid=@Userid and [Date]=@Yesterday order by id desc
OriginalGriff 9-Feb-13 8:06am    
Simplify it a bit:
i = -1;
Loop while i > -10
i = -2
...
i = i - 1
End loop
Is what you are doing at present. The line at the bottom of the loop is irrelevant because you overwrite the value at the top of the loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900