Since I hate asking for specific solutions, here's the background for my question in case I'm approaching it the wrong way. Skip to the bold line if you want to skip the background and get right to the question. I am creating a PDF form that will be prepopulated with data from an SQL database. In order to do this, I need to export the data from the database into an XFDF file, which I can then easily merge with the blank PDF form to fill in the values.
An XFDF file has this format:
<fields>
<field name="FirstName">
<value>John</value>
</field>
<field name="LastName">
<value>Smith</value>
</field>
</fields>
Where "FirstName" and "LastName" correspond to names of fields on the PDF form, and "John" and "Smith" are the corresponding values from the database to populate the form with.
So my goal is to create the XFDF file, and have the names of the fields on the PDF correspond to the names of the columns in the table. My current approach is to use a SELECT FOR XML EXPLICIT statement. To use that, I first need to select the data into the "Universal Table" format that the for xml explicit statement requires, which would have a separate row for each column in my original data table. I could manually go through each field, writing a separate select statement for each column- so for example, the FirstName column part would look something like:
SELECT 2 AS Tag,
1 AS Parent,
null AS [fields!1],
FirstName AS [fields!2!name],
D.FirstName AS [value!3!]
From Data AS D
(I am not sure that's exactly the right syntax for the select for XML statement, but it should be something like that)
If I did a select statement like that for each column in my original table (plus one for the root level "fields" element, and then unioned them together, it should create the table I need to then do the SELECT FOR XML EXPLICIT statement.
However, there are a ton of fields on this form (government agencies! Yeesh!) and I'd prefer some sort of way to do this programmatically.
So now that I've given the background, here's my actual question. If you read the background and think my approach sucks, let me know.
I need a way to take a table containing a row like this:
+-----------+----------+-----+ | FirstName | LastName | Etc | +-----------+----------+-----+ | John | Smith | 123 | +-----------+----------+-----+
and construct a new table from it that looks like this:
+-----------+-------+ | FieldName | Value | +-----------+-------+ | FirstName | John | | LastName | Smith | | Etc | 123 | +-----------+-------+
If this were valid, I'd do it like this:
SELECT 2 AS Tag,
1 AS Parent,
null AS [fields!1],
C.column_name AS [fields!2!name],
D.{C.column_name} AS [value!3!]
From Data AS D, information_schema.columns AS C
WHERE C.table_name = 'Data'
Obviously that is not possible, and you can't make the column being selected variable. I looked at this question ( Use variable Column name in Select statement on SQL server 2008 ) and the top answer might be promising but it ends with "But it's all pretty horrible, IMHO." Before embarking down that road, I'd like to look into solutions that aren't "pretty horrible". Any suggestions?