FOR XML PATH(column) does not work. Is there any workaround?

iCyanide

New Member
I need to address an issue to have a column value being part of the element root like this (having the user ids 45 and 46 as root element for each record): \[code\]<U> <45> <FirstName>John</FirstName> <LastName>Smith</LastName> <Address>OneDirection</Address> </45> <46> <FirstName>Lisa</FirstName> <LastName>Simpson</LastName> <Address>OtherDirection</Address> </46></U>\[/code\]I'am trying the FOR XML Path approach but I'm not able to figure out how can I indicate the query I just want the UserID to be displayed at element root. Here is what I've able to find until now: \[code\]DECLARE @User TABLE( UserID int , FirstName nvarchar(40) , LastName nvarchar(40) , [Address] nvarchar(40))INSERT INTO @User VALUES (45, 'John', 'Smith', 'OneDirection')INSERT INTO @User VALUES (46, 'Lisa', 'Simpson', 'OtherDirection')SELECT UserID as "@UserID" , FirstName , LastName , [Address]FROM @UserFOR XML PATH('IDoNotWantThisHere'), ROOT('U') \[/code\]This is the output:\[code\]<U> <IDoNotWantThisHere UserID="45"> <FirstName>John</FirstName> <LastName>Smith</LastName> <Address>OneDirection</Address> </IDoNotWantThisHere> <IDoNotWantThisHere UserID="46"> <FirstName>Lisa</FirstName> <LastName>Simpson</LastName> <Address>OtherDirection</Address> </IDoNotWantThisHere></U>\[/code\]Any comment on this will be really helpful. Thanks in advance.
 
Back
Top