|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SVF in CLR with Nullable Parameterscreated 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...
Show quote
"I/Gear" <IG***@discussions.microsoft.com> wrote in message Inside SQL Server use the SQL Server native types for nullability support.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!"); > } [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 =?Utf-8?B?SS9HZWFy?= <IG***@discussions.microsoft.com> wrote in
Show quote news:C7E6A01F-086A-4780-9CE8-BC1710B89074@microsoft.com: As David replies in his post; you are using CLR's DateTime type, which > 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. 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: That's because the deployment functionality does not understand the new > > [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: 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 ************************************************** |
|||||||||||||||||||||||