SQL 如何在存储过程中增加一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13947472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:42:19  来源:igfitidea点击:

how to increment a value inside stored procedure

sqlsql-serverstored-procedures

提问by Prathiesh

How to increment a value of local variable in the loop inside a stored procedure

如何在存储过程内的循环中增加局部变量的值

ALTER PROC [dbo].[Usp_SelectQuestion]
@NoOfQuestion int
AS
BEGIN
Declare @CNT int
Declare @test int
Declare @x int
Declare @y int
set @x = 1;
set @y = 1; 
Select @CNT=(Select Count(*) from (select Distinct(setno)from onlin) AS A)
select @test=@NoOfQuestion/@CNT
while @x <= @CNT do 
    while @y <= @test
        select  * from onlin where setno = @x
        set @y = @y +1
    set @x =@x + 1 
END

the values like @xand @yis not incrementing and I am stuck in an infinite loop.

@x和这样的值@y没有增加,我陷入了无限循环。

回答by alzaimar

you have to enclose the while-body in a begin-end block.

您必须将 while-body 包含在开始结束块中。

while @x <= @CNT do begin
  while @y <= @test begin
    select  * from onlin where setno = @x
    set @y = @y + 1
  end
set @x =@x + 1
end

I do not understand what you are trying to achive (besides the fact that you want to increase some variables)

我不明白你想要达到什么(除了你想增加一些变量的事实)