I have been struggling with converting a SQL table elements into a 3 tiered XML table. I have it almost there but I know I am just missing one small piece somewhere.

Here is what I am looking for:

```
<labels _FORMAT="\US Labels\ChemSamples.lwl" _QUANTITY="1" _PRINTERNUMBER="10">
  <label>
    <variable name="HBANumber">1234</variable>
    <variable name="BatchNumber">567</variable>
    <variable name="BagNumber">1</variable>
    <variable name="CombinedBarcodeTop">123601</variable>
    <variable name="CombinedBarcodeText">123601</variable>
    <variable name="HBANumberSmall">1234</variable>
    <variable name="CombinedSmall">123601</variable>
    <variable name="CombinedBarcodeBottom">123601</variable>
    <variable name="CreatedDate">02/10/2015</variable>
    <variable name="Operator">7197</variable>
    <variable name="PrintDate">02/10/2015</variable>
  </label>
</labels>

```

I have create a temp table to generate the data and my SQL query looks like this:

```
declare @tmpTable table(fieldname varchar(25), fieldvalue varchar(20))

insert into @tmpTable values('HBANumber','1234')
insert into @tmpTable values('BatchNumber','567')
insert into @tmpTable values('BagNumber','1')
insert into @tmpTable values('CombinedBarcodeTop','123601')
insert into @tmpTable values('CombinedBarcodeText','123601')
insert into @tmpTable values('HBANumberSmall','1234')
insert into @tmpTable values('CombinedSmall','123601')
insert into @tmpTable values('CombinedBarcodeBottom','123601')
insert into @tmpTable values('CreatedDate','02/10/2015')
insert into @tmpTable values('Operator','7197')
insert into @tmpTable values('PrintDate','02/10/2015')
SELECT
    '\US Labels\ChemSamples.lwl' AS '@_FORMAT',
    1 AS '@_QUANTITY',
    10 AS '@_PRINTERNUMBER',
    (select null,
        (select fieldname AS '@name', fieldvalue as 'variable'
            from @tmpTable
            for XML path ('variable'), type)
    for XML PATH ('label'), TYPE)
FOR XML PATH ('labels')

```

Executing this query returns the following results:

```
<labels _FORMAT="\US Labels\ChemSamples.lwl" _QUANTITY="1" _PRINTERNUMBER="10">
  <label>
    <variable name="HBANumber">
      <variable>1234</variable>
    </variable>
    <variable name="BatchNumber">
      <variable>567</variable>
    </variable>
    <variable name="BagNumber">
      <variable>1</variable>
    </variable>
    <variable name="CombinedBarcodeTop">
      <variable>123601</variable>
    </variable>
    <variable name="CombinedBarcodeText">
      <variable>123601</variable>
    </variable>
    <variable name="HBANumberSmall">
      <variable>1234</variable>
    </variable>
    <variable name="CombinedSmall">
      <variable>123601</variable>
    </variable>
    <variable name="CombinedBarcodeBottom">
      <variable>123601</variable>
    </variable>
    <variable name="CreatedDate">
      <variable>02/10/2015</variable>
    </variable>
    <variable name="Operator">
      <variable>7197</variable>
    </variable>
    <variable name="PrintDate">
      <variable>02/10/2015</variable>
    </variable>
  </label>
</labels>

```

Does anyone have an idea on what I can do to get it to look like the first item I listed?

Thanks