Using a SELECT statement in MS SQL 2005 TSQL, and no source table, just variables as listed below…
@FirstName varchar(20)
@LastName varchar(20)
@ZipCode varchar(5)
…what syntax will build XML resembling the following?
<XMLDATA><REC FirstName="JOHN" LastName="SMITH" ZipCode="98052" /></XMLDATA>
How about this:
The
FOR XML PATH('REC')defines the XML tag and theAS '@FirstName'specifies you want this value to be an attribute on that XML tag. TheROOT('XMLDATA')wraps the whole thing into yet another XML Tag on the outside.