Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have a table with variable columns?

Tags:

sql

sql-server

It might be a stupid question, but here goes:

Is it possible to make a dynamic table that's able to contain rows with variable number of columns and custom column names?

I have glanced over EAV-modelling, but it seems heavy. A real life example could be this:

Let's say I have a register with customers. But each customer might have different information to be entered. And depending on what you want to enter, it should be reflected in the database. (I.E. every customer has different columns)

Is this impossible/probable?

Update:

The standard approach (i.e. having a table with all needed columns and saving information only into columns that make sense for a particular customer while setting the remaining ones to NULL) doesn't work for me because what I want can't use 'fixed' column names. Example one customer might want CVR-number and another might want their phonenumber as a reference number. And a third might want some completely different information. So to avoid having a table containing 500 columns, I have now thought of making an extra table containing rows of column-data. Like so: Id, Name, Value, CustomerId. So when I want information for a customer, all I have to do is to iterate through this table with a specific customer Id.

my own edit!:

Sorry for troubling you with this simple SQL-issue! :-) Have a nice day...

like image 404
KristianB Avatar asked Sep 05 '11 08:09

KristianB


People also ask

Is it possible to refer to columns of a table variable?

A variable is actually a Constant. You cannot change it once it is stored. In addition, you cannot refer a column from a variable table like TableVar[ProductKey].

Is it possible to have a column table and variable all with using the table you created in the question above writing PL SQL block to test your theory?

It is possible to have variables with the same name in SQL and PL/SQL but it is almost certainly a bad idea to do so. Using variables with the same name will lead to confusing scope issues.

How do you create a variable table in SQL?

To declare a table variable, start the DECLARE statement. The name of table variable must start with at(@) sign. The TABLE keyword defines that used variable is a table variable. After the TABLE keyword, define column names and datatypes of the table variable in SQL Server.

What is a variable table?

A variable table is an object that groups multiple variables. All the global parameters (now named variables) that you use in workload scheduling are contained in at least one variable table.


2 Answers

You could model this as a one-to-many relationship between a Customer and a CustomerAttributes table. Something like:

**Customer table**
CustomerId
LastName
FirstName
...

**CustomerAttributes table**
CustomerId
AttributeName
AttributeValue
like image 164
Paolo Falabella Avatar answered Sep 23 '22 23:09

Paolo Falabella


This is not possible in Sql-Server. As Marco says, you can store each customer's data in xml.

If all the columns are known ahead of time and some customers use one set and other customers use a different set, then sub-tables with each set of columns is the normal approach.

If the columns are not known ahead of time, then how would the data even be used? No code or reports could refer to it. Perhaps it should be stored unstructured in a general purpose 'Notes' field.

like image 40
DevDelivery Avatar answered Sep 22 '22 23:09

DevDelivery