I am using SQL server 2008 and I’m quite new to writing sql. My aim is to export data from a table into xml format to create a CAP xml file that can be used in our website. Currently, I’m just writing some select statements to retrieve data in the correct format. Here is the code:
select (SELECT TOP 5 [Master_Incident_Number] AS incidents
,[Jurisdiction] AS jurisdiction
,[Response_Date] AS Date
FROM [ESCAD_DW_System].[dbo].[CurrentIncidents_V] Incident
FOR XML PATH ('area'), type ) AS Alert for xml path (''),
ROOT ('?xml version = "1.0" encoding = "UTF-8"?')
However, I am getting ‘invalid XML identifier’ error for ‘?’ symbol. Can anyone help?
You cannot use
ROOTto add an xml encoding tag. The only way to do that is to convert the xml output intovarchar(max)and prepend with your encoding tag. Keep in mind though thatFOR XMLoutput is UTF-16 by default, and therefore your UTF-8 encoding is probably unnecessary. Having said that, here is a simple example that uses a UDF to convert the xml intovarchar(max):Result:
Further reading:
http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx
http://msdn.microsoft.com/en-us/library/ms345137%28v=sql.90%29.aspx