0

Hi and thank you for your time in advance. I am fairly new to formating XML output from a SQL query, so here it goes :) I need to format my FOR XML SQL query (in SSMS 2012) to match this format:

<HR_Data>
    <ClientInfo>
      <OrganizationOID>XXXX</OrganizationOID>
      <ClientId>XXXX</ClientId>
    </ClientInfo>
    <EmployeeHRData>
      <Employee_ID>XXXX</Employee_ID>
      <SocialSecurityNumber>XXXX</SocialSecurityNumber>
      ....
    </EmployeeHRData>
    <EmployeeHRData>
      <Employee_ID>XXXX</Employee_ID>
      <SocialSecurityNumber>XXXX</SocialSecurityNumber>
      ....
    </EmployeeHRData>
</HRData>

So in words, a ClientInfo header with info specific to the company, and then an EmployeeHRData section that repeats for each employee, all wrapped in a HRData tag.

So far, I have the following output(Client info repeats, instead of appearing only once, and have a FinalOutputTable tag that doesn't belong):

<HR_Data>
  <FinalOutputTable>
    <ClientInfo>
      <OrganizationOID>XXXX</OrganizationOID>
      <ClientId>XXXX</ClientId>
    </ClientInfo>
    <EmployeeHRData>
      <Employee_ID>XXXX</Employee_ID>
      <SocialSecurityNumber>XXXX</SocialSecurityNumber>
      ....
    </EmployeeHRData>
  </FinalOutputTable>
  <FinalOutputTable>
    <ClientInfo>
      <OrganizationOID>XXXX</OrganizationOID>
      <ClientId>XXXX</ClientId>
    </ClientInfo>
    <EmployeeHRData>
      <OrganizationOID>XXXX</OrganizationOID>
      <ClientId>XXXX</ClientId>
      ....
    </EmployeeHRData>
  </FinalOutputTable>
</HRData>

Which is generated from:

SELECT (SELECT 'XXXX' AS 'OrganizationOID', 'XXXX' AS 'ClientId'
        FOR XML PATH (''), TYPE) ClientInfo

        ,(SELECT [AOID] AS AssociateOID
                ,ISNULL([Employee Identifier], '') AS Employee_ID
                ,ISNULL(SSN, '') AS SocialSecurityNumber
                ,.........

        FOR XML PATH (''), TYPE) EmployeeHRData            


INTO #FinalOutputTable
FROM XXXX
WHERE XXXX
ORDER BY XXXX

SELECT * FROM #FinalOutputTable AS FinalOutputTable
FOR XML AUTO, ROOT('HR_Data'), ELEMENTS

1 Answer 1

1

To have a nested ClientId element inside the ClientInfo element you use

Select 11 as "ClientInfo/ClientId"

So you can use the forward slash to define the location for the value in the xml result.

EDIT: you can use the code below if you want to return a document for one client.

Declare @HR_Data xml
Set @HR_Data=
(Select HR_Data.ClientInfo
 from
(
Select  (
     SELECT 'XXXX' AS 'OrganizationOID', 'XXXX' AS 'ClientId'
     FOR XML PATH (''), TYPE
    ) ClientInfo
) HR_Data 
FOR XML PATH(''), ROOT('HR_Data'))


Declare @EmployeeHRData xml
Set @EmployeeHRData=
(SELECT   
      [AOID] AS AssociateOID
      ,ISNULL([Employee Identifier], '') AS Employee_ID
      ,ISNULL(SSN, '') AS SocialSecurityNumber
       ,.........
       from table_name
   FOR XML PATH('EmployeeHRData'), TYPE
   )


SET @HR_Data.modify(
    'insert sql:variable("@EmployeeHRData")
    as last into (/HR_Data[1])')
Select @HR_Data
Sign up to request clarification or add additional context in comments.

2 Comments

I am confused. The ClientID is currently nested inside the Client Info element. Currently the ClientInfo tag is repeated for each employee, and I need the ClientInfo element to only appear once at the very top.
I got what your are asking for now. I have updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.