SQL SERVER 求階乘之和

景上發表於2016-06-05

求階乘之和:

CREATE PROCEDURE proc_jc
@num INT
AS
BEGIN
  DECLARE @cj INT,@sum INT,@i INT;
  SELECT @cj=1,@sum=0,@i=1;
  WHILE @i<=@num
    BEGIN
       SET @cj=@cj*@i;
       SET @sum=@sum+@cj;
       SET @i=@i+1;
    END
   PRINT CAST(@num AS VARCHAR(30))+'的階乘之和是'+CAST(@sum AS VARCHAR(50));
END

執行儲存過程:

EXEC proc_jc 3;

刪除儲存過程:

DROP PROCEDURE proc_jc;

相關文章