Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServer 2008 generic column type

I've a table on SqlServer where a column type should be undefined, I mean, it should be able to contain any kind of type (string, int, boolean and datetime).

This because my users should be able to define which type the column is for each record. In the past when I encountered this problem I decided to create it as nvachar(MAX). When I accessed the value I automatically casted the string to the correct type. I know that this method wastes a lot of memory and has a performance implications.

Maybe is possible to create a different table for each type but I never had the time to test that.

Do you have any thoughts?

Thanks!

like image 573
adospace Avatar asked Dec 31 '25 18:12

adospace


1 Answers

Database designs, where variant type of data is stored into columns will almost always come back and be a major issue later on. The quality of data almost always suffers.

I have seen this lead to non-atomic data (comma separated values), badly formed date values in string fields, multi-use of a single column and a host of other problems.

I would suggest that you look into what it is that they want to store and make the appropriate database design with properly normalized tables.

Having ranted (and thank you for listening), you can use SQL_Variant for your purposes. Read more here:

http://msdn.microsoft.com/en-us/library/ms173829.aspx

like image 116
Raj More Avatar answered Jan 03 '26 10:01

Raj More



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!