日期:2014-05-18 浏览次数:20585 次
create table tb1(no char(1)) insert into tb1 select 1 union select 2 union select 3 union select 4 union select 5 select * from tb1 union select a.no+b.no from tb1 a,tb1 b where a.no>b.no union select a.no+b.no+c.no from tb1 a,tb1 b,tb1 c where a.no>b.no and b.no>c.no union select a.no+b.no+c.no+d.no from tb1 a,tb1 b,tb1 c,tb1 d where a.no>b.no and b.no>c.no and c.no>d.no union select '12345' /* no ----- 1 12345 2 21 3 31 32 321 4 41 42 421 43 431 432 4321 5 51 52 521 53 531 532 5321 54 541 542 5421 543 5431 5432
------解决方案--------------------
CREATE FUNCTION FN_C(
@N INT
,@R INT
)
RETURNS INT
AS
BEGIN
IF @N<= 0
RETURN 1
DECLARE @RET INT
DECLARE @I INT
SET @I = 1
SET @RET = 1
WHILE @I <= @R
BEGIN
SET @RET = @RET * (@N - @I + 1) / @I
SET @I = @I + 1
END
RETURN @RET
END
GO
SELECT DBO.FN_C(5,1)+DBO.FN_C(5,2)+DBO.FN_C(5,3)+DBO.FN_C(5,4)+DBO.FN_C(5,5)
-- 结果
31
------解决方案--------------------
动态的语句懒得写了