views:

20

answers:

2

I would like to have a vertical table (I know, I know... it's pretty much unavoidable) that is able to store an identifying field, the value, and the original type. Obviously the value field needs to be something generic or I won't be able to store various different types of data in it (varchar, text, int, decimal, bit, etc).

What suggestions do you have for this type of setup that would allow me to not lose precision on number data types while offering flexibility and ease of use?

+1  A: 

Your best bet is definitely a VARCHAR column, given that there are pretty standard mechanisms to convert most types to/from a String.

Please bear in mind that every time someone makes a table like this, a kitten dies. Seriously, find a better way.

Tom
+1, because people need to know what they're doing to teh kitties
Philip Kelley
Slightly more seriously, I've wondered whether varbinary(max) might not be better. Wouldn't everything be castable to binary and back without loss?
Philip Kelley
It would, but if you ever needed to cast your eye over the table's contents it would be fairly meaningless. A little transparency is often worth a few extra bytes.
Tom
A: 

You're going to be storing the data vertically, but for the most part, aren't you going to end up taking certain types of values and breaking them out horizontally anyway? I've seen systems where they have generic type columns of : varchar, int, float, date.

It will take some more coding to determine which column to use for what field; otherwise, you'll just have to deal with converting to another format.

Jeff O