Search this insane blog:

Monday, February 9, 2009

Select your data as XML Format (SQL Server)

Converting the data you already have inside your database

  • You can create a select statement as you would already do, with a FOR XML at the end of your select statement
  • Options (a la carte):

SELECT * FROM employees FOR XML auto


FOR XML RAW

The element defaults to "row" (can further customize)


<row fistcolumn ="blah" secondcolumn="blah"/>;

FOR XML AUTO

The element name is called by

<employees fistcolumn="blah" secondcolumn="blah" />

FOR XML PATH

Xml elements are parsed based on column

<row>

<firstColumn>blah</firstColumn>

<secondColumn>blah</secondColumn>
<row>


FOR EXPLICIT (explained):

Here is a select statement (same as above, but with the gory details to the EXPLICIT option

SELECT 1 AS Tag, NULL AS Parent, employeeID AS [employees!1!id], employee_fname AS[employees!1!employee_fname]
FROM employees
FOR XML EXPLICIT, ROOT('blah')

FOR XML EXPLICIT

FOR XML EXPLICIT requires the second column to hold NULL or nonnegative integers that represent XML parent tag IDs.