欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » Sql随手笔记 » 正文

热门阅读

在sql server中打印九九乘法表

创建时间:2012年05月28日 14:55  阅读次数:(16208)
分享到:
今天在csdn上看到几位朋友写的利用sql语句打印九九乘法表的方式,比较有意思。
方法一:
declare @x int
set @x=1
declare @y int
declare @c varchar(8000)

while(@x<=9)
begin
 select @y=1,@c=''
 while(@y<=@x)
 begin
   select @c=@c+cast(@y as varchar)+'*'+cast(@x as varchar)+'='+cast(@x*@y as varchar)+' '
   set @y=@y+1
 end
 print @c+char(10)
 set @x=@x+1
end

这个方法打印出来的效果如下:
1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 

方法二:
select
max(case when a>0 then '1*'+a+'='+ltrim(a*1) end) [1],
max(case when a>1 then '2*'+a+'='+ltrim(a*2) end) [2],
max(case when a>2 then '3*'+a+'='+ltrim(a*3) end) [3],
max(case when a>3 then '4*'+a+'='+ltrim(a*4) end) [4],
max(case when a>4 then '5*'+a+'='+ltrim(a*5) end) [5],
max(case when a>5 then '6*'+a+'='+ltrim(a*6) end) [6],
max(case when a>6 then '7*'+a+'='+ltrim(a*7) end) [7],
max(case when a>7 then '8*'+a+'='+ltrim(a*8) end) [8],
max(case when a>8 then '9*'+a+'='+ltrim(a*9) end) [9]
from   
(
    select ltrim(number) a from master..spt_values 
    where type='p' and number between 1 and 9
) t group by a

这个sql查询的结果为:
1*1=1 NULL NULL NULL NULL NULL NULL NULL NULL
1*2=2 2*2=4 NULL NULL NULL NULL NULL NULL NULL
1*3=3 2*3=6 3*3=9 NULL NULL NULL NULL NULL NULL
1*4=4 2*4=8 3*4=12 4*4=16 NULL NULL NULL NULL NULL
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 NULL NULL NULL NULL
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 NULL NULL NULL
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 NULL NULL
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 NULL
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

利用C#实现可以参考本站:利用C#实现九九乘法表
来源:
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 4+0=? 的结果(结果是:4)
结  果: