Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Columns sharing the same data space

Author  Topic 

michaels-r
Starting Member

4 Posts

Posted - 2008-07-16 : 15:12:27
Hi - I am having total brain fade tonight and cannot remember how to make multiple cols of different types share the same data space. e.g. you have val_type which specifies the type of value being stored and can access the data using val_int or val_char etc. where val_int and val_char occupy the same space in the table.

Thanks,

Michael

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-16 : 15:29:58
I don't understand what you mean. Could you provide a data example?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

michaels-r
Starting Member

4 Posts

Posted - 2008-07-16 : 16:04:52
If I could give you an example it would not be a problem! However, I will try to explain again:

You have a table with a column defining a data type of either: 'Integer' or 'Char'
A further column contains the data: Value
However, rather than putting a value into the column Value, I want to have 2 fields: Value_i and Value_c pointing to the same physical location (2 aliases for the Value column with different data types)... have I just answered my own question or is there another way, similar to the C/C++ union?

Thanks, Michael
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-16 : 16:10:46
Are you referring to a computed column?

I don't know what a union is in either c or c++.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

michaels-r
Starting Member

4 Posts

Posted - 2008-07-16 : 17:18:49
No, not a computed column, a native one.

A union in C++ is a structure where there are 2 or more names for the same variable, all of which have different types but occupy the same memory space. Generally you will set the value and access it using the same name at a given time. It's essentially a mechanism to allow indirection - which is what I am trying to achieve here but may be completely off the mark.

Simplifying the problem, what I am trying to do is create a table with 2 columns:

CREATE TABLE T1
( ValueI Int,
ValueD Decimal
);

But have ValueI and ValueD actually be the same column, sharing the same storage space. The idea being that another reference table will dictate what the actual type is i.e which name to use to access the data.

Thanks, Michael
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-16 : 17:22:14
That is not possible.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

michaels-r
Starting Member

4 Posts

Posted - 2008-07-16 : 20:10:21
OK - thanks. I did start to wonder if my memory was failing me.... :-)
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-07-16 : 21:49:30
That would be achieveable using computed column, but in the context of a db design forum I have to ask why.
Is this some dodgy attempt at 'typifying' as EAV by any chance?
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-07-16 : 23:23:18
>> have ValueI and ValueD actually be the same column, sharing the same storage space.

Do you mean sql_variant type?
Go to Top of Page
   

- Advertisement -