SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is it possible to make an SQLCLR data type 'comparable'?


Is it possible to make an SQLCLR data type 'comparable'?

Author
Message
StarNamer
StarNamer
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2774 Visits: 1992
I have a C# project to implement a complex number type and wanted to execute
SELECT DISTINCT c1 FROM complex


This gives the error:
Msg 421, Level 16, State 1, Line 1
The ComplexNumber data type cannot be selected as DISTINCT because it is not comparable.


I then implemented the IComparable interface for the C# struct, but this made no difference.

Is there a way to make a CLR implemented data type recognized as 'comparable' to SQL?

I've already found that there doesn't seem to be a way to overload operators in SQL so I have to do
declare @c ComplexNumber
declare @d ComplexNumber

set @c = '1+0i'
set @d = @c

select @c.Plus(@d).ToString()
// Result is 2+0i


So I won't be surprised if the answer is a flat "No".

I also found that
SELECT c1 FROM complex GROUP BY c1

gives
Msg 249, Level 16, State 1, Line 2
The type "ComplexNumber" is not comparable. It cannot be used in the GROUP BY clause.

As expected! Smile

So, as an alternative, is there some way to do the equivalent of 'SELECT DISTINCT' or 'SELECT ... GROUP BY' in a set based way when using CLR defined types?

By the way, I'm actually using SQLserver 2012 (not 2005) but decided this was the most appropriate forum.

Derek
StarNamer
StarNamer
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2774 Visits: 1992
It seems the solution is to make sure the type is binary comparable and to set IsByteOrdered = true in the arguments to the Microsoft.SqlServer.Server.SqlUserDefinedType attribute.

Of course, the problem then is to ensure that the binary format is correctly ordered!

Derek
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39658 Visits: 14412
Derek Dongray (2/14/2013)
Of course, the problem then is to ensure that the binary format is correctly ordered!

Need any assistance on this piece or have you found what you needed?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
asdw1
asdw1
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 1
Sorry for mining, but i run into the same problem. My udt must have own serialization and i don't how to implement (if it is possible) my own procedure for byte ordering. Thanks for help in advance.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search