刚学到SQL的while循环 现在想实现如下,输出结果为:
*
**
***
****
*****
虽然语法和C#差不多,就是不知道用T-SQL语句如何实现 尤其是那个换行符,哪位知道啊?讲下如何输出吧!

解决方案 »

  1.   

    declare @i int
    set @i=1
    while @i<6
    begin
    print REPLICATE('*',@i)
    set @i=@i+1
    end*
    **
    ***
    ****
    *****
      

  2.   

    这个是带换行符declare @i int
    declare @s varchar(500)
    set @s=''
    set @i=1
    while @i<6
    begin
    set @s = @s + REPLICATE('*',@i)+char(13)
    set @i=@i+1
    endselect @s*
    **
    ***
    ****
    *****
    (所影响的行数为 1 行)
      

  3.   

    给你个 最近学的脚本 输出菱形declare @N int,@M int
    set @M=0
    set @N=0while @M<7
    begin 
    if @M<=3/*输出图形的上半部分*/
    begin 
    while @N<4
    begin 
    print space(8-@N)+REPLICATE('*',@N*2+1)
    SET @N=@N+1
    end
    SET @M=@M+1
    end
    ELSE/*输出图形的下半部分*/
    BEGIN 
    while @n>0
    begin 
    set @N=@N-1;
    print SPACE(8-@N)+REPLICATE('*',@n*2+1);
    end
    set @M=@M+1
    END
    end
    /*
            *
           ***
          *****
         *******
         *******
          *****
           ***
            *
    */
      

  4.   

    DECLARE @l INT
    DECLARE @c VARCHAR(30)
    SET @c=''
    SET @l=5
    WHILE(@l>0)
    BEGIN
    SET @c=@c+'*'
    PRINT @c
    SET @l=@l-1
    END
    /*
    *
    **
    ***
    ****
    *****
    */
    print 输出时自动换行
      

  5.   

    declare @n int,@s varchar(500)
    select @n=1,@s=''
    while @n<=5
    begin
    select @s=@s + replicate('*',@n)+ char(10)
    set @n=@n+1
    endselect @s
    /**
    **
    ***
    ****
    *****
    (1 行受影响)
    */