for存储过程中的循环 [英] for loop in stored procedure

查看:75
本文介绍了for存储过程中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在存储过程中使用while循环。但是当我在storedprocedure中使用while循环时,数据没有插入.i没有任何错误。但它停止工作。

如何使用while循环在sp?

i试试这个...



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.

推荐答案

没有。它不会退出,是吗?

你这样做有一点意义:

Well no. It won''t exit, will it?
There is little point in you doing this:
select	@i=@i-1

在循环的底部,如果位于顶部,则执行以下操作:

At the bottom of your loop if right at the top you do this:

select top 1 @id=id from Biometrics where Userid=@Userid and [Date]=@Yesterday order by id desc

并且您不会更改WHILE子句中的任何细节!如果它工作一次,它将每次都设置相同的值!

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!


这篇关于for存储过程中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆