mercredi 4 mars 2015

How to generate complex xml form ms sql server

I am trying to have following xml file from MS sql servier. I am not sure how to generate this kind of xml from sql query.


Please share advise



<retail:customerPreference xmlns:core="http://ift.tt/1AJ0J7p" xmlns:hta="http://ift.tt/1MSGXQD" xmlns:htd="http://ift.tt/1r9rtvs" xmlns:htt="http://ift.tt/1MSGXQG" xmlns:ns10="http://ift.tt/1AJ0JnL" xmlns:ns11="http://ift.tt/1BHQ9mq" xmlns:ns12="http://ift.tt/1AJ0JnP" xmlns:ns13="http://ift.tt/1BHQaH2" xmlns:ns7="http://www.btor.com/crm" xmlns:ns9="http://ift.tt/1AJ0Lfo" xmlns:retail="http://ift.tt/1BHQaH4" xmlns:sref="http://ift.tt/1DbQpGQ" xmlns:tools="http://ift.tt/1BHQaH6" xmlns:xsi="http://ift.tt/ra1lAU">
<retail:created>2015-03-03T13:42:55Z</retail:created>
<retail:optionPathId>Email:Email Optin</retail:optionPathId>
<retail:optionSetId type="customerPreferenceOptionSet" optionSetId="ContactPermissions">
<retail:groupId groupHierarchyId="All" groupTypeId="region">All</retail:groupId>
</retail:optionSetId>
<retail:customerId>0001505741504</retail:customerId>
<retail:lastUpdated>2015-03-03T13:43:03Z</retail:lastUpdated>
<retail:preferenceId>44e6-:ebcdd8fdb41:fbc61655-:24b6c309b24b4b57</retail:preferenceId>
<retail:value id="Email:Email Optin">true</retail:value>


SQL script



;
WITH XMLNAMESPACES('http://ift.tt/ra1lAU' AS xsi, 'http://ift.tt/1BHQaH4' AS retail, 'http://ift.tt/1AJ0J7p' AS core)
select (select
'Email:Email Optin' as [retail:optionPathId],
(select 'All' as [retail:groupId groupHierarchyId="All" optionSetId="region"]
For xml path(''), type, ROOT('retail:optionSetId type="customerPreferenceOptionSet" optionSetId="ContactPermissions"'))
,'123' as [retail:customerId]
,'true' as [retail:value id= "Email:Email Optin"]
For xml path(''), type, ROOT('retail:customerPreference'))
,
Opt_Out_Mail,
Opt_Out_SMS,
Opt_Out_Telephone,
contact_id
from [dbo].[Permissions]
For XML AUTO, ELEMENTS

Aucun commentaire:

Enregistrer un commentaire