Could someone please help me with an XML output template please. I have been requested by a client to create an xml output file. This file will then feed in to Client's CRM. So that's why, it has to be an exact match of Client's requested template. I have managed to match it perfectly apart from CDATA for couple of fields.
below is the query you can use for testing purposes. I need CDATA wrap for fields Client and Area. I have also attached the output i am getting by running below code
If OBJECT_ID('tempdb..#Temp') is Not NULL
Drop table #Temp
CREATE TABLE #Temp
(
[ShiftDate] [date] NULL,
[Ref_Num] [varchar](20) NULL,
[Agency_Worker_Name] [varchar](100) NULL,
[Client] [varchar](100) NULL,
[Area] [VarChar] (100) Null,
[Assignment] [varchar](20) NULL,
[Contract_Start] [varchar](30) NULL,
[Contract_End] [varchar](30) NULL,
[Contract_BreakInMinutes] [varchar](10) NULL,
[Contract_Total] [varchar](30) NULL,
[Actual_Start] [varchar](30) NULL,
[Actual_End] [varchar](30) NULL,
[Actual_BreakInMinutes] [varchar](10) NULL,
[Actual_Total] [varchar](30) NULL,
[Commission] [decimal](18, 2) NULL,
[Total_Cost] [decimal](18, 2) NULL,
[Rate] [varchar](20) NULL,
[OverallCost] [decimal](18, 2) NULL,
[AgencybackingReport] [int] NULL,
[AccountCode] [varchar](20) NULL
)
Insert Into #Temp
Values
('2018-07-24',
'83076641',
'ABCD',
'ABCD',
'ABCD',
'CPA00',
'09:00',
'17:00',
'30',
'07:30',
'10:30',
'17:00',
'30',
'05:30',
'28.49',
'159.01',
'Basic',
'221.59',
'1220883',
' ABCD')
Declare @xml Int=(Select max(AgencyBackingReport) From #Temp)
select @xml As [@AgencyBackingReport],(Select
[Ref_Num] As [@reference],
[ShiftDate] as [@startdate],
Case When [AccountCode] is NULL Then 'Unknown' Else [AccountCode] End as [@accountcode],
Contract_Start As 'PlannedShift/Start',
Contract_End As 'PlannedShift/End',
Contract_BreakInMinutes As 'PlannedShift/BreakinMinutes',
Actual_Start As 'ActualShift/Start',
Actual_End As 'ActualShift/End',
Actual_BreakInMinutes As 'ActualShift/BreakinMinutes',
OverallCost As [OverallCost],
Agency_Worker_Name As 'AdditionalInformation/WorkerName',
Client As 'AdditionalInformation/Client',
Area As 'AdditionalInformation/Area',
-- ( select
-- 1 as Tag ,
-- 0 as Parent ,
-- (Select
-- Area
-- From #Temp M2
-- Where M1.Ref_Num = m2.Ref_Num)
-- As [Area!1!!CDATA]
-- for xml explicit
--) As 'AdditionalInformation/Area',
Assignment As 'AdditionalInformation/Assignment',
Commission As 'AdditionalInformation/Commission',
Total_Cost As 'AdditionalInformation/TotalCost',
Rate As 'AdditionalInformation/Rate'
From #Temp M1
for xml path('Shift'),Type)
for XML Path('Shifts'),Type