We have a situation where we need to store form data in our sql server but each new job we setup will have different fields with different field names and lengths. An example
Job 1:
Field 1: first_name - varchar(20)
Field 2: last_name - varchar(30)
Job 2:
Field 1: first_name - varchar(15)
Field 2: middle_initial - varchar(1)
Field 3: last_name - varchar(30)
Initially we were setting up separate tables to store this data which conformed exactly to the form in question. But it lead to a maintenance nightmare as there were so many tables, procs, dts, ssis packages to be changing each time in order to accomodate the dynamic nature of this data.
We came up with a different solution to store all of the data in xml fields thus solving most of the problem. It now exists similar to this.
<Record>
<first_name>value</first_name>
<last_name>value</last_name>
</Record>
Then we would create views to pull this data out of the table
SELECT
, IsNull(data.value('(/Record/first_name)[1]', 'varchar(20)'),'') as first_name
, IsNull(data.value('(/Record/last_name)[1]', 'varchar(30)'),'') as last_name
FROM FormTable
Now this is much better than we had before but it also means that we still need to create the custom view each time. I'd much rather maintain some type of table that lists the fields and will build that query for me.
Field Name | Field Type | Field Length
first_name | varchar | 20
last_name | varchar | 30
I'm pretty sure I cannot create a dynamic view. One option that could work is a table valued function. But is there something that I am overlooking here? Would there be better options to be able to dynamically store the data in this way (without moving away from SQL SERVER since i know other databases such as CouchDB will do this natively.)