1

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

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

@x@y不递增的值,我陷入了无限循环。

4

1 回答 1

3

您必须将 while-body 包含在 begin-end 块中。

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

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

于 2012-12-19T07:35:41.330 回答