Solved

SQL query to produce XML

Posted on 2014-07-22
1
252 Views
Last Modified: 2014-07-28
Hello,

I have an SQL statement that reads some rows from a couple of tables and produces an xml.  See below:
 declare @xmlData xml ;

  -- Now select all the data for the given id in xml format.
  set @xmlData = 
    ( 
		
    select top 5

       L.[Id],
       L.[UserId],
	   Un.[Name] as [UnitName],
       left([LastLoginDateTime],16) as [LastLoginDateTime] ,
       left( [LastLogoutDateTime], 16) as [LastLogoutDateTime],
       [LastLoginIP],
	   case when [LastLogoutReason] = 1 then 'Loggat ut' when [LastLogoutReason] = 2 then 'Utloggat av systemet' else '' end as [LastLogoutReason]

    from [BRUM_Admin].[dbo].[Logins] L 
    inner join [BRUM_Admin].[dbo].Units Un on l.UnitId = Un.Id
     where userid = 1
      and LastLoginDateTime is not null
    order by LastLoginDateTime desc
 
         
          for xml path ('OriginalData2012'), type)
 
  select @xmlData

Open in new window

and it produces the following xml:
<OriginalData2012>
  <Id>16097</Id>
  <UserId>1</UserId>
  <UnitName>nasher</UnitName>
  <LastLoginDateTime>2014-02-17 21:52</LastLoginDateTime>
  <LastLogoutDateTime>2014-02-17 22:52</LastLogoutDateTime>
  <LastLoginIP>90.213.111.23</LastLoginIP>
  <LastLogoutReason>Loggat ut</LastLogoutReason>
</OriginalData2012>
<OriginalData2012>
  <Id>1</Id>
  <UserId>1</UserId>
  <UnitName>EyeNetAdmin</UnitName>
  <LastLoginDateTime>2013-11-17 21:52</LastLoginDateTime>
  <LastLogoutDateTime>2013-11-17 22:08</LastLogoutDateTime>
  <LastLoginIP>90.230.64.247</LastLoginIP>
  <LastLogoutReason>Utloggat av systemet</LastLogoutReason>
</OriginalData2012>

Open in new window

However i would like to produce an xml like this:
<OriginalData2012>
  <Id1>16097</Id1>
  <UserId1>1</UserId1>
  <UnitName1>nasher</UnitName1>
  <LastLoginDateTime1>2014-02-17 21:52</LastLoginDateTime1>
  <LastLogoutDateTime1>2014-02-17 22:52</LastLogoutDateTime1>
  <LastLoginIP1>90.213.111.23</LastLoginIP1>
  <LastLogoutReason1>Loggat ut</LastLogoutReason1>

  <Id2>1</Id2>
  <UserId2>1</UserId2>
  <UnitName2>EyeNetAdmin</UnitName2>
  <LastLoginDateTime2>2013-11-17 21:52</LastLoginDateTime2>
  <LastLogoutDateTime2>2013-11-17 22:08</LastLogoutDateTime2>
  <LastLoginIP2>90.230.64.247</LastLoginIP2>
  <LastLogoutReason2>Utloggat av systemet</LastLogoutReason2>
</OriginalData2012>

Open in new window

Basically each node name should be qualified with its order in the xml so that they are unique.

Any ideas?

Thanks in advance!
0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40211151
you cannot get that in the standard sql server "for XML" feature.
the tags are row-based, hence the 2 xml tags.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question