views:

69

answers:

1

I have this user defined function.

public partial class UserDefinedFunctions
{
    static int i;
    [SqlFunction(IsDeterministic = true)]
    public static SqlSingle f()
    {
        return new SqlSingle(1.3F);
    }
};

But it only works if i is readonly. Why?

+2  A: 

It also 'works' if is read-write.

If by 'not works' you mean the assembly cannot be installed in SQL because it contains a static field, use of static class variables is dangerous for reasons explained in CLR Hosted Environment:

Given these considerations, we discourage the use of static variables and static data members of classes used in SQL Server. For SAFE and EXTERNAL_ACCESS assemblies, SQL Server examines the metadata of the assembly at CREATE ASSEMBLY time and fails the creation of such assemblies if it finds the use of static data members and variables.

Mark your assembly as UNSAFE as a sign that you understand the risks associated with static variables and SQL will accept your assembly.

Remus Rusanu