Listing 1

using System;
using System.Data.Sql;
using System.Data.SqlTypes;

public partial class UserDefinedFunctions
{
  [SqlFunction]
  public static SqlString Factorial()
  {
    // Put your code here
    return "Hello";
  }
};

Listing 2

using System;
using System.Data.Sql;
using System.Data.SqlTypes;

public partial class UserDefinedFunctions
{
  [SqlFunction]
  public static SqlInt64
              Factorial(SqlInt32 Operand)
  {
    if (Operand <= 0)
      return 1;
    else
      return (Operand *
              Factorial(Operand - 1));
  }
};

Listing 3

sp_configure 'clr_enabled', 1
reconfigure

CREATE ASSEMBLY MyFuncs
FROM 'C:\Projects\CLRIntegration\bin\Debug\
CLRIntegration.dll'
WITH PERMISSION_SET = SAFE

CREATE FUNCTION Factorial(@Operand INT)
RETURNS BIGINT AS
EXTERNAL NAME
MyFuncs.UserDefinedFunctions.Factorial

Listing 4

CREATE FUNCTION Factorial(@Operand INT)
RETURNS BIGINT
AS
BEGIN
  DECLARE @Result BIGINT

  IF (@Operand <= 1)
    SET @Result = 1
  ELSE
    SET @Result = @Operand *
          dbo.Factorial(@Operand - 1)

  RETURN @Result
END