34,575
社区成员
发帖
与我相关
我的任务
分享
DECLARE @NumA INT=1
DECLARE @NumB INT=1
DECLARE @Str VARCHAR(200)
WHILE @NumA<=9
BEGIN
SET @Str=''
SET @NumB=1
WHILE @NumB<=@NumA
BEGIN
SET @Str=@Str+RTRIM(@NumB)+'*'+RTRIM(@NumA)+'='+RTRIM(RIGHT(100+@NumA*@NumB,2))+SPACE(1)
SET @NumB=@NumB+1
END
SET @Str = REPLACE(@Str, '=0', '= ')
PRINT @Str
SET @NumA=@NumA+1
END
PRINT CHAR(13)
/*
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
*/
SET @NumA=1
WHILE @NumA<=9
BEGIN
SET @Str=''
SET @NumB=@NumA
WHILE @NumB>0
BEGIN
SET @Str=@Str+RTRIM(@NumB)+'*'+RTRIM(@NumA)+'='+RTRIM(RIGHT(100+@NumA*@NumB,2))+SPACE(1)
SET @NumB=@NumB-1
END
SET @Str = SPACE((9-@NumA)*7)+REPLACE(@Str, '=0', '= ')
PRINT @Str
SET @NumA=@NumA+1
END
PRINT CHAR(13)
/* 1*1= 1
2*2= 4 1*2= 2
3*3= 9 2*3= 6 1*3= 3
4*4=16 3*4=12 2*4= 8 1*4= 4
5*5=25 4*5=20 3*5=15 2*5=10 1*5= 5
6*6=36 5*6=30 4*6=24 3*6=18 2*6=12 1*6= 6
7*7=49 6*7=42 5*7=35 4*7=28 3*7=21 2*7=14 1*7= 7
8*8=64 7*8=56 6*8=48 5*8=40 4*8=32 3*8=24 2*8=16 1*8= 8
9*9=81 8*9=72 7*9=63 6*9=54 5*9=45 4*9=36 3*9=27 2*9=18 1*9= 9
*/
SET @NumA=9
WHILE @NumA>0
BEGIN
SET @Str=''
SET @NumB=1
WHILE @NumB<=9
BEGIN
IF @NumA>=@NumB
SET @Str=@Str+RTRIM(@NumB)+'*'+RTRIM(@NumA)+'='+RTRIM(RIGHT(100+@NumA*@NumB,2))+SPACE(1)
SET @NumB=@NumB+1
END
SET @Str =REPLACE(@Str, '=0', '= ')
PRINT @Str
SELECT @Str='', @NumA=@NumA-1
END
PRINT CHAR(13)
/*
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
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*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*4= 4 2*4= 8 3*4=12 4*4=16
1*3= 3 2*3= 6 3*3= 9
1*2= 2 2*2= 4
1*1= 1
*/
SET @NumA=9
WHILE @NumA>0
BEGIN
SET @Str=''
SET @NumB=@NumA
WHILE @NumB>0
BEGIN
SET @Str=@Str+RTRIM(@NumA)+'*'+RTRIM(@NumB)+'='+RTRIM(RIGHT(100+@NumA*@NumB,2))+SPACE(1)
SET @NumB=@NumB-1
END
SET @Str =SPACE((9-@NumA)*7)+REPLACE(@Str, '=0', '= ')
PRINT @Str
SELECT @Str='', @NumA=@NumA-1
END
/*
9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1= 9
8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1= 8
7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1= 7
6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1= 6
5*5=25 5*4=20 5*3=15 5*2=10 5*1= 5
4*4=16 4*3=12 4*2= 8 4*1= 4
3*3= 9 3*2= 6 3*1= 3
2*2= 4 2*1= 2
1*1= 1
*/
(9 行受影响)
(9 行受影响)
one two three four five six seven eight nine
---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1X1=1 NULL NULL NULL NULL NULL NULL NULL NULL
1X2=2 2X2=4 NULL NULL NULL NULL NULL NULL NULL
1X3=3 2X3=6 3X3=9 NULL NULL NULL NULL NULL NULL
1X4=4 2X4=8 3X4=12 4X4=16 NULL NULL NULL NULL NULL
1X5=5 2X5=10 3X5=15 4X5=20 5X5=25 NULL NULL NULL NULL
1X6=6 2X6=12 3X6=18 4X6=24 5X6=30 6X6=36 NULL NULL NULL
1X7=7 2X7=14 3X7=21 4X7=28 5X7=35 6X7=42 7X7=49 NULL NULL
1X8=8 2X8=16 3X8=24 4X8=32 5X8=40 6X8=48 7X8=56 8X8=64 NULL
1X9=9 2X9=18 3X9=27 4X9=36 5X9=45 6X9=54 7X9=63 8X9=72 9X9=81
(9 行受影响)
if object_id('Tempdb..#t') is not null drop table #t
create table #t(
[id] int identity(1,1) not null,
[one] varchar(100) null,
[two] varchar(100) null,
[three] varchar(100) null,
[four] varchar(100) null,
[five] varchar(100) null,
[six] varchar(100) null,
[seven] varchar(100) null,
[eight] varchar(100) null,
[nine] varchar(100) null
)
Insert Into #t(one)
select null from master..spt_values where type='P' and number between 1 and 9
update #t
set one=case when id>=1 then '1X'+cast(id as varchar(2))+'='+cast(id as varchar(2)) end,
two=case when id>=2 then '2X'+cast(id as varchar(2))+'='+cast((id*2) as varchar(2)) end,
three=case when id>=3 then '3X'+cast(id as varchar(2))+'='+cast((id*3) as varchar(2)) end,
four=case when id>=4 then '4X'+cast(id as varchar(2))+'='+cast((id*4) as varchar(2)) end,
five=case when id>=5 then '5X'+cast(id as varchar(2))+'='+cast((id*5) as varchar(2)) end,
six=case when id>=6 then '6X'+cast(id as varchar(2))+'='+cast((id*6) as varchar(2)) end,
seven=case when id>=7 then '7X'+cast(id as varchar(2))+'='+cast((id*7) as varchar(2)) end,
eight=case when id>=8 then '8X'+cast(id as varchar(2))+'='+cast((id*8) as varchar(2)) end,
nine=case when id>=9 then '9X'+cast(id as varchar(2))+'='+cast((id*9) as varchar(2)) end
select one,two,three,four,five,six,seven,eight,nine from #t
你们写sql这么久,有谁想到楼主的问题呀
呵呵,你才是牛人呢
DECLARE @a smallint, @b smallint, @str varchar(max) set @a=1 while @a<=20 BEGIN set @b=1 set @str='' while @b<=@a begin select @str=@str+convert(varchar(max),@b)+'x'+convert(varchar(max),@a)+'='+convert(varchar(max),@a*@b)+space(2) set @b=@b+1 end print @str set @a=@a+1 end /* 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 1x10=10 2x10=20 3x10=30 4x10=40 5x10=50 6x10=60 7x10=70 8x10=80 9x10=90 10x10=100 1x11=11 2x11=22 3x11=33 4x11=44 5x11=55 6x11=66 7x11=77 8x11=88 9x11=99 10x11=110 11x11=121 1x12=12 2x12=24 3x12=36 4x12=48 5x12=60 6x12=72 7x12=84 8x12=96 9x12=108 10x12=120 11x12=132 12x12=144 1x13=13 2x13=26 3x13=39 4x13=52 5x13=65 6x13=78 7x13=91 8x13=104 9x13=117 10x13=130 11x13=143 12x13=156 13x13=169 1x14=14 2x14=28 3x14=42 4x14=56 5x14=70 6x14=84 7x14=98 8x14=112 9x14=126 10x14=140 11x14=154 12x14=168 13x14=182 14x14=196 1x15=15 2x15=30 3x15=45 4x15=60 5x15=75 6x15=90 7x15=105 8x15=120 9x15=135 10x15=150 11x15=165 12x15=180 13x15=195 14x15=210 15x15=225 1x16=16 2x16=32 3x16=48 4x16=64 5x16=80 6x16=96 7x16=112 8x16=128 9x16=144 10x16=160 11x16=176 12x16=192 13x16=208 14x16=224 15x16=240 16x16=256 1x17=17 2x17=34 3x17=51 4x17=68 5x17=85 6x17=102 7x17=119 8x17=136 9x17=153 10x17=170 11x17=187 12x17=204 13x17=221 14x17=238 15x17=255 16x17=272 17x17=289 1x18=18 2x18=36 3x18=54 4x18=72 5x18=90 6x18=108 7x18=126 8x18=144 9x18=162 10x18=180 11x18=198 12x18=216 13x18=234 14x18=252 15x18=270 16x18=288 17x18=306 18x18=324 1x19=19 2x19=38 3x19=57 4x19=76 5x19=95 6x19=114 7x19=133 8x19=152 9x19=171 10x19=190 11x19=209 12x19=228 13x19=247 14x19=266 15x19=285 16x19=304 17x19=323 18x19=342 19x19=361 1x20=20 2x20=40 3x20=60 4x20=80 5x20=100 6x20=120 7x20=140 8x20=160 9x20=180 10x20=200 11x20=220 12x20=240 13x20=260 14x20=280 15x20=300 16x20=320 17x20=340 18x20=360 19x20=380 20x20=400 */