输出指定格式的数据列

-- =============================================

-- Author:    maco_wang

-- Create date: 2011-03-30

-- Description:  

-- 需求贴:http://topic.csdn.NET/u/20110330/10/dd155c82-e156-49df-9b5a-65bdbb0bf3ab.html

-- =============================================

前记:

Csdn上看到一帖子,要求如下:

编程一个函数实现功能,给出n,打印1-n,例如1 22 33 444 555 666 7777 8888 9999 10101010

就是要

1个1位: 1
2个2位: 22 33
3个3位: 444 555 666
4个4位: 7777 8888 9999 10101010

....

虽然是.NET技术-ASP.NET板块的帖子,但是思路都是一样的,用SQL写了一下:

create function PrintN(@n int)

returns @table table (id bigint)

as

begin

    declare @i bigint;set @i=1

    declare @j bigint;declare @k bigint;

    while (@i<=@n)

       begin

           set @j=0;set @k=0

           while @j<@i

           begin

              set @j=@j+@k;set @k=@k+1

           end

       insert into @table select replicate(@i,@k-1)

       set @i=@i+1

       end

    return

end

 

--查看结果

select * from dbo.PrintN(20)

/*

1

22

33

444

555

666

7777

8888

9999

10101010

1111111111

1212121212

1313131313

1414141414

1515151515

161616161616

171717171717

181818181818

191919191919

202020202020

*/

 
 
原文地址:https://www.cnblogs.com/accumulater/p/6244738.html