T-SQL程序设计
1. 求1+2+3+...+100的总和
DECLARE @I INT ,@SUM INT
SET @I=1
SET @SUM=0
WHILE (@I<=100)
BEGIN
SET @SUM=@SUM+@I
SET @I=@I+1
END
PRINT @SUM
2. 求10!
DECLARE @I INT ,@MUTI BIGINT
SET @I=1
SET @MUTI=1
WHILE (@I<=10)
BEGIN
SET @MUTI=@MUTI*@I
SET @I=@I+1
END
PRINT @MUTI
3. 计算1!+2!+…+100!的总和
DECLARE @I FLOAT, @J FLOAT,@SUM FLOAT, @FACTORAIL FLOAT
SET @I=1
SET @J=1
SET @SUM=0
SET @FACTORAIL=1
WHILE ( @I<=100 )
BEGIN
WHILE ( @J<=@I )
BEGIN
SET @FACTORAIL=@J*@FACTORAIL
SET @J=@J+1
END
SET @SUM=@SUM+@FACTORAIL
SET @I=@I+1
SET @J=1
SET @FACTORAIL=1
END
PRINT @SUM
4. 九九乘法表
select
[1] = case when a>0 then '1*'+ltrim(a)+'='+ltrim(a*1) end,
[2] = case when a>1 then '2*'+ltrim(a)+'='+ltrim(a*2) end,
[3] = case when a>2 then '3*'+ltrim(a)+'='+ltrim(a*3) end,
[4] = case when a>3 then '4*'+ltrim(a)+'='+ltrim(a*4) end,
[5] = case when a>4 then '5*'+ltrim(a)+'='+ltrim(a*5) end,
[6] = case when a>5 then '6*'+ltrim(a)+'='+ltrim(a*6) end,
[7] = case when a>6 then '7*'+ltrim(a)+'='+ltrim(a*7) end,
[8] = case when a>7 then '8*'+ltrim(a)+'='+ltrim(a*8) end,
[9] = case when a>8 then '9*'+ltrim(a)+'='+ltrim(a*9) end
from (select number a
from master..spt_values
where type='p' and number between 1 and 9
) t
go
/*
select number from master..spt_values
where type='p'
0~2047 共2048个数字