Home All Groups Group Topic Archive Search About

SVF in CLR with Nullable Parameters

Author
21 Dec 2005 10:36 PM
I/Gear
I'm just learning to use the new CLR integration features of SQL 2005 and I
created the following simple function:

    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,
SystemDataAccess=SystemDataAccessKind.None)]
    public static SqlString Foo(DateTime dtStartDate, DateTime dtEndDate)
    {
         return new SqlString("Hello, World!");
    }

Everything works correctly until I pass a NULL value to the function.  So, I
changed the function to accept Nullable parameters as follows:

    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,       
SystemDataAccess=SystemDataAccessKind.None)]
    public static SqlString DateDiff(Nullable<DateTime> dtStartDate,
Nullable<DateTime> dtEndDate)
    {
         return new SqlString("Hello, World!");
    }

Everything compiles correctly, but when I attempt to deploy the assembly I
receive the following error:

Error    1    Incorrect syntax near '`'.

If I remove the Nullable<> option, everything works correctly.

Anyone have any input on this?

Thanks...

Author
22 Dec 2005 11:52 AM
David Browne
Show quote
"I/Gear" <IG***@discussions.microsoft.com> wrote in message
news:C7E6A01F-086A-4780-9CE8-BC1710B89074@microsoft.com...
> I'm just learning to use the new CLR integration features of SQL 2005 and
> I
> created the following simple function:
>
>    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,
> SystemDataAccess=SystemDataAccessKind.None)]
>    public static SqlString Foo(DateTime dtStartDate, DateTime dtEndDate)
>    {
>         return new SqlString("Hello, World!");
>    }
>
> Everything works correctly until I pass a NULL value to the function.  So,
> I
> changed the function to accept Nullable parameters as follows:
>
>    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,
> SystemDataAccess=SystemDataAccessKind.None)]
>    public static SqlString DateDiff(Nullable<DateTime> dtStartDate,
> Nullable<DateTime> dtEndDate)
>    {
>         return new SqlString("Hello, World!");
>    }

Inside SQL Server use the SQL Server native types for nullability support.

  [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,
SystemDataAccess=SystemDataAccessKind.None)]
  public static SqlString Foo(SqlDateTime dtStartDate, SqlDateTime
dtEndDate)
  {
    if (dtStartDate.IsNull || dtEndDate.IsNull)
    {
      return SqlString.Null;
    }
    DateTime start = dtStartDate.Value;
    DateTime end = dtEndDate.Value;


    return new SqlString(end.ToString);
  }


David
Author
27 Dec 2005 8:57 AM
Niels Berglund
=?Utf-8?B?SS9HZWFy?= <IG***@discussions.microsoft.com> wrote in
Show quote
news:C7E6A01F-086A-4780-9CE8-BC1710B89074@microsoft.com:

> I'm just learning to use the new CLR integration features of SQL 2005
> and I created the following simple function:
>
>     [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,
> SystemDataAccess=SystemDataAccessKind.None)]
>     public static SqlString Foo(DateTime dtStartDate, DateTime
>     dtEndDate) {
>          return new SqlString("Hello, World!");
>     }
>
> Everything works correctly until I pass a NULL value to the function.


As David replies in his post; you are using CLR's DateTime type, which
is a value type and not nullable. Change instead to use (as David says)
the SqlDateTime type. They live in the SqlTypes namespace and they are
nullable and also completely isomorphic with the underlying T-SQL types,
i.e. they need no converssion so you'll get better performance out of
them (probably not noticeble, but still). In fact, when writing SQLCLR
methods you should always use SqlTypes.


> So, I changed the function to accept Nullable parameters as follows:
>
>     [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,    
>      
> SystemDataAccess=SystemDataAccessKind.None)]
>     public static SqlString DateDiff(Nullable<DateTime> dtStartDate,
> Nullable<DateTime> dtEndDate)
>     {
>          return new SqlString("Hello, World!");
>     }
>
> Everything compiles correctly, but when I attempt to deploy the
> assembly I receive the following error:

That's because the deployment functionality does not understand the new
nullable types.

Niels



--
**************************************************
* Niels Berglund
* http://staff.develop.com/nielsb
* nielsb@no-spam.develop.com
* "A First Look at SQL Server 2005 for Developers"
* http://www.awprofessional.com/title/0321180593
**************************************************

AddThis Social Bookmark Button