if not object_id('factorial') is null drop function factorial Go create function factorial (@n int) returns bigint AS begin declare @i int,@result bigint set @i=1 set @result=1 while @i<=@n begin set @result=@result*@i set @i=@i+1 end return @result end GO select dbo.factorial(4) /* -------------------- 24(1 個資料列受到影響)*/
create function jc(@i int) returns int as begin declare @j int select @j = isnull(@j,1) * a.number from master..spt_values a where a.type = 'p' and a.number > 0 and a.number <= @i return @j end select dbo.jc(6)--720
if not object_id('factorial') is null
drop function factorial
Go
create function factorial
(@n int)
returns bigint
AS
begin
declare @i int,@result bigint
set @i=1
set @result=1
while @i<=@n
begin
set @result=@result*@i
set @i=@i+1
end
return @result
end
GO
select dbo.factorial(4)
/*
--------------------
24(1 個資料列受到影響)*/
returns int
as
begin
declare @j int
select
@j = isnull(@j,1) * a.number
from master..spt_values a
where a.type = 'p' and a.number > 0 and a.number <= @i
return @j
end
select dbo.jc(6)--720