Reputation: 2518
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?
Upvotes: 0
Views: 287
Reputation: 294197
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
andEXTERNAL_ACCESS
assemblies, SQL Server examines the metadata of the assembly atCREATE 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.
Upvotes: 2