【叶子函数分享五十三】输出指定格式的数据列

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

-- 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

就是要

11位: 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

*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值