DECLARE @Fact TABLE(Nums INT)
INSERT INTO @Fact
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 3 UNION ALL
SELECT 5 UNION ALL
SELECT 10
SELECT * FROM @Fact
--Solution :
DECLARE @Fact TABLE(Nums INT)
INSERT INTO @Fact
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 3 UNION ALL
SELECT 5 UNION ALL
SELECT 10
DECLARE @no INT,@f BIGINT,@nums INT
DECLARE C1 CURSOR FOR SELECT nums FROM @fact
OPEN C1
FETCH NEXT FROM C1 INTO @no
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @f=1,@nums=@no
WHILE @no>0
BEGIN
SET @f=@f*@no
SET @no=@no-1
END
DECLARE @t TABLE(nums INT,Fact BIGINT)
INSERT INTO @T SELECT @Nums,@F
FETCH NEXT FROM C1 INTO @no
END
CLOSE C1
DEALLOCATE C1
SELECT * FROM @T ORDER BY 1
No comments:
Post a Comment