日期:2014-05-17  浏览次数:20509 次

SQL循环内再加循环问题
SQL code
DECLARE @i INT,
    @h INT,@Nun INT
SET @i = 0 
SET @h = 0
SET @Nun = 1
WHILE @i < 5 /*循环一*/
    BEGIN 
        WHILE @h < 5/*循环二*/   
            BEGIN
                PRINT  @Nun /*循环一加循环二这里输出应该是25次才对,但他只输出了5次。*/
                SET @Nun=@Nun+1
                SET @h = @h + 1 
            END
        SET @i = @i + 1 
    END


像上面怎样能让他在循环二也运行五次,双循环好像内循环只运行一次

------解决方案--------------------
SQL code
DECLARE @i INT,
    @h INT,@Nun INT
SET @i = 0 
--SET @h = 0
SET @Nun = 1
WHILE @i < 5 /*循环一*/
    BEGIN 
        SET @h = 0
        WHILE @h < 5/*循环二*/   
            BEGIN
                PRINT  @Nun /*循环一加循环二这里输出应该是25次才对,但他只输出了5次。*/
                SET @Nun=@Nun+1
                SET @h = @h + 1 
            END
        SET @i = @i + 1 
    END

------解决方案--------------------
SQL code
DECLARE @i INT,
    @h INT,@Nun INT
SET @i = 0 
SET @Nun = 1
WHILE @i < 5 /*循环一*/
    BEGIN
        SET @h = 0
        WHILE @h < 5/*循环二*/   
            BEGIN
                PRINT  @Nun /*循环一加循环二这里输出应该是25次才对,但他只输出了5次。*/
                SET @Nun=@Nun+1
                SET @h = @h + 1 
            END
        SET @i = @i + 1 
    END
/*1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*/