[SQLServer]簡單的遞迴範例

  • 1023
  • 0

摘要:[SQLServer]簡單的遞迴範例

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function dbo.udf_calfactorial
(@n int = 1)
returns decimal(38,0)
with returns null on null input 
as
begin 
	return (case when @n <= 0  then null 
			     when @n > 1  then 
				 cast(@n as float) * dbo.udf_calfactorial(@n-1) 
				 when @n = 1 then 1 end );
end;

select dbo.udf_calfactorial(3)

簡單的遞迴例子,參考Pro T-SQL 2012電子書