T-SQL Filter XML Variable - sql

I was wondering which one of the following two was the better way of filtering on a column in my xml variable.
INSERT INTO [Tracking].[Team]([Name], [Description], [Begin_Dt], [End_Dt], [Update_Dt])
SELECT
x.[Team].value('Name', 'varchar(100)') AS [Name],
x.[Team].value('Description', 'varchar(250)') AS [Description],
x.[Team].value('Begin_Dt', 'datetime') AS [Begin_Dt],
x.[Team].value('End_Dt', 'datetime') AS [End_Dt],
getdate() as [Update_Dt]
FROM #xml.nodes('/Team') x([Team])
WHERE x.[Team].value('Team_Ref_ID', 'int') = 0
INSERT INTO [Tracking].[Team]([Name], [Description], [Begin_Dt], [End_Dt], [Update_Dt])
SELECT
x.[Team].value('Name', 'varchar(100)') AS [Name],
x.[Team].value('Description', 'varchar(250)') AS [Description],
x.[Team].value('Begin_Dt', 'datetime') AS [Begin_Dt],
x.[Team].value('End_Dt', 'datetime') AS [End_Dt],
getdate() as [Update_Dt]
FROM #xml.nodes('/Team') x([Team])
WHERE x.[Team].exist('Team_Ref_ID[. = 0]') = 1
Notice the WHERE clause, one uses exist, the other uses value, or is there a third method that's more effective ?
Thanks,
Raul

For querying an #xml variable the best improvement you can do is to have it be of type with an XML schema, see Typed XML Compared to Untyped XML. This will benefit the XPath expressions in your query (the .nodes('/Team'), the various .value and the .exists operators). But there will be an upfront cost in validating the schema when the variable is assigned.
The other typical XML performance improvement is XML indexes, but unfortunately they cannot be applied to variables so the point is mute in regard to your problem.
As to your particular minutia question (whether the .value('Team_Ref_ID') is faster than .exists('Team_Ref_ID[. = 0]')) I think the former is faster, but I have no evidence and I may well be wrong.

Actually what i found to be better was this
INSERT INTO [Tracking].[Team]([Name], [Description], [Begin_Dt], [End_Dt], [Update_Dt])
SELECT
x.[Team].value('Name', 'varchar(100)') AS [Name],
x.[Team].value('Description', 'varchar(250)') AS [Description],
x.[Team].value('Begin_Dt', 'datetime') AS [Begin_Dt],
x.[Team].value('End_Dt', 'datetime') AS [End_Dt],
getdate() as [Update_Dt]
FROM #xml.nodes('/Team[Team_Ref_Id = 0]') x([Team])
It filters out the nodes in the xml parser instead of having to filter them out in the SQL.

Related

Data from XML to MSSQL using CROSS APPLY

I'm having trouble extracting some data from a XML file to MSSQL DB using CROSS APPLY, here is what I have:
Note: I did read about how to post here but pardon me if I'm still wrong.
XML File:
<?xml version="1.0" encoding="UTF-8" ?>
<ZMPROD01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<DOCNUM>0000003899888135</DOCNUM>
<CREDAT>20220201</CREDAT>
<CRETIM>152041</CRETIM>
</EDI_DC40>
<ZPROD SEGMENT="1">
<WERKS>8285</WERKS>
<LGNUM>0</LGNUM>
<AUFNR>000915229446</AUFNR>
<LINENO>RM01PL01</LINENO>
<CHARG>0006186588</CHARG>
<START1>20220202</START1>
<START2>211609</START2>
<QTY>4166.000</QTY>
<END1>20220202</END1>
<END2>240000</END2>
<MAKTX>579 FUS5 75ML ULTRA SENST GEL</MAKTX>
<PLN_ORDER>6963701111</PLN_ORDER>
<Z1PRODI SEGMENT="1">
<POSNR>000010</POSNR>
<MATNR>000000000098920665</MATNR>
</Z1PRODI>
<Z1PRODI SEGMENT="1">
<POSNR>000040</POSNR>
<HRKFT>V010</HRKFT>
</Z1PRODI>
<Z1PRODI SEGMENT="1">
<POSNR>000050</POSNR>
<MATNR>000000000099396964</MATNR>
</Z1PRODI>
</ZPROD>
</IDOC>
</ZMPROD01>
My SQL query:
INSERT INTO XMLTESTTABLE(PONo, ASP, LOTNo, EntryDate, StartDate, EndDate, GAS, PlannedQty, LineNum, SAPDesc, StartTime, EndTime)
SELECT
MY_XML.ZPROD.query('AUFNR').value('.', 'VARCHAR(9)'),
MY_XML.ZPROD.query('CHARG').value('.', 'VARCHAR(8)'),
MY_XML.ZPROD.query('PLN_ORDER').value('.', 'VARCHAR(10)'),
MY_XML.ZPROD.query('START1').value('.', 'date'),
MY_XML.ZPROD.query('START1').value('.', 'date'),
MY_XML.ZPROD.query('END1').value('.', 'date'),
MY_XML.ZPROD.query('CHARG').value('.', 'VARCHAR(8)'),
MY_XML.ZPROD.query('QTY').value('.', 'VARCHAR(9)'),
MY_XML.ZPROD.query('LINENO').value('.', 'VARCHAR(1)'),
MY_XML.ZPROD.query('MAKTX').value('.', 'VARCHAR(9)'),
MY_XML.ZPROD.query('START2').value('.', 'time'),
MY_XML.ZPROD.query('END2').value('.', 'time')
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'C:\Users\PC_user\Documents\Idoc3899888135.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('ZMPROD01/ZPROD') AS MY_XML (ZPROD);
The result I get is (0 rows affected). I tried replacing ZMPROD01/ZPROD several times but different errors appeared, at some point complained about being unable to convert to date datatype.
On a separate note how, can I get data from EDI_DC40 as well? Not sure how the CROSS APPLY would look like to look for different places on the document.
Your help is appreciated.
Thanks.
Please try the following solution.
Notable points:
I adjusted the XPath expression in the .nodes() method.
No need to use the .query() method.
text() is added for performance reasons.
Last two data elements converted into TIME data type.
As it was already mentioned, the <END2>240000</END2> is not a legit
value for the TIME data type.
SQL
--INSERT INTO XMLTESTTABLE(PONo, ASP, LOTNo, EntryDate, StartDate, EndDate, GAS, PlannedQty, LineNum, SAPDesc, StartTime, EndTime)
SELECT ZPROD.value('(AUFNR/text())[1]', 'VARCHAR(9)')
, ZPROD.value('(CHARG/text())[1]', 'VARCHAR(8)')
, ZPROD.value('(PLN_ORDER/text())[1]', 'VARCHAR(10)')
, ZPROD.value('(START1/text())[1]', 'date')
, ZPROD.value('(START1/text())[1]', 'date')
, ZPROD.value('(END1/text())[1]', 'date')
, ZPROD.value('(CHARG/text())[1]', 'VARCHAR(8)')
, ZPROD.value('(QTY/text())[1]', 'VARCHAR(9)')
, ZPROD.value('(LINENO/text())[1]', 'VARCHAR(1)')
, ZPROD.value('(MAKTX/text())[1]', 'VARCHAR(9)')
, TRY_CAST(STUFF(STUFF(ZPROD.value('(START2/text())[1]', 'CHAR(6)'),3,0,':'),6,0,':') AS TIME)
, TRY_CAST(STUFF(STUFF(ZPROD.value('(END2/text())[1]', 'CHAR(6)'),3,0,':'),6,0,':') AS TIME)
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'e:\Temp\Idoc3899888135.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('/ZMPROD01/IDOC/ZPROD') AS MY_XML (ZPROD);
Your path seems wrong - what about:
CROSS APPLY MY_XML.nodes('ZMPROD01/IDOC/ZPROD') AS MY_XML (ZPROD);
You seem to have forgotten the IDOC
And the data type is because you cannot convert the times to the data type time. Try starting with the select where everything is converted to varchar, then start changing the data tyoes, and you will find the errors
SELECT CAST('211609' AS TIME)
returns error

how to use GETDATE() function with xml exist() function

I have a scenario where i have to use xml exist() function in where clause of the query. where i have to compare date from the xml with the current date. When i try to use GETDATE() function, i get below error.
The argument 1 of the XML data type method "exist" must be a string literal.
Please consider below query for your reference.
SELECT
TRY_CONVERT(datetime,NULLIF(t.x.value('(./Expire)[1]','varchar(max)'), '')) as expiration_date
FROM VW_Analytics_Base_Facts(nolock) BaseFact
CROSS APPLY BaseFact.Fact.nodes ('/Fact/Grant') t(x)
WHERE TYPE = '/asset/portfolio/option'
AND BaseFact.Fact.exist('./Expire[(text()[1] cast as xs:date?) le xs:date("' + CONVERT(NVARCHAR(max),CONVERT(date,GETDATE()))+'")]')=1
Thanks in advance.
Below is the updated working query.
DECLARE #tbl TABLE (XmlCol xml)
INSERT INTO #tbl VALUES
('<option>
<OptionName>Option 1</OptionName>
<grant>
<GrantName>Grant 1</GrantName>
<schedules>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>1/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>2/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>3/1/2018</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
</schedules>
</grant>
<grant>
<GrantName>Grant 2</GrantName>
<schedules>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>1/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>2/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
<schedule>
<scheduleID></scheduleID>
<scheduleName></scheduleName>
<scheduleDate>3/1/2019</scheduleDate>
<scheduleAmount></scheduleAmount>
</schedule>
</schedules>
</grant>
</option>'
)
SELECT e.XmlCol.value('(/option/OptionName)[1]', 'varchar(100)'),
t.x.value('../.././GrantName[1]','varchar(100)') GrantName,
t.x.value('(./scheduleDate)[1]', 'varchar(100)') scheduleDate
FROM #tbl e
CROSS APPLY (SELECT CONVERT(date,GETDATE())) dt(today)
cross apply e.XmlCol.nodes ('/option/grant/schedules/schedule') t(x)
WHERE e.XmlCol.exist('./scheduleDate[(text()[1] cast as xs:date?) le sql:column("dt.today")]')=1
i want to filter the record based on schedule date.
This query is a part of view so i cannot declare a variable.
Next time please append a stand-alone working sample with DDL, data inserts, your own code and the expected output. This would allow to test a solution.
In this case I have to guess and post untested:
SELECT
TRY_CONVERT(datetime,NULLIF(t.x.value('(./Expire)[1]','varchar(max)'), '')) as expiration_date
FROM VW_Analytics_Base_Facts(nolock) BaseFact
CROSS APPLY (SELECT CONVERT(date,GETDATE())) dt(today)
CROSS APPLY BaseFact.Fact.nodes ('/Fact/Grant') t(x)
WHERE TYPE = '/asset/portfolio/option'
AND BaseFact.Fact.exist('./Expire[(text()[1] cast as xs:date?) le sql:column("dt.today")]')=1;
This query will use one more CROSS APPLY in order to include the information in the resultset. The function sql:column() allows to use a result set's column within XQuery without breaking the must-be-a-literal rule (use sql:variable() to get hands on a declared variable's value).
UPDATE
Your idea to apply the filter as early as possible is not wrong. This was even better as predicate within .nodes(). But you have to rely on an implicit cast, something you should not do.
The expression (text()[1] cast as xs:date?) will use the system's culture. A value like 1/3/2018 might be taken as 1st of March or as 3rd of January. Therefore I'd suggest to read the value as string and use CONVERT with the appropriate style:
Btw: I use two times CROSS APPLY with .nodes() to avoid backward navigation (../../)
WITH DerivedTable AS
(
SELECT e.XmlCol.value('(/option/OptionName/text())[1]', 'varchar(100)') AS OptionName
,gr.value('(GrantName/text())[1]','varchar(100)') GrantName
,sch.value('(scheduleID/text())[1]', 'varchar(100)') scheduleID --use appropriate type, might be "int"
,sch.value('(scheduleName/text())[1]', 'varchar(100)') scheduleName
,CONVERT(DATE,sch.value('(scheduleDate/text())[1]', 'varchar(100)'),110) scheduleDate --fetch this as varchar, to avoid implicit casts
--use CONVERT with the appropriate style to get a real date
--110 is mdy, 103 is dmy
,sch.value('(scheduleAmount/text())[1]', 'varchar(100)') scheduleAmount --use appropriate type, might be "decimal(12,4)"
FROM #tbl e
CROSS APPLY e.XmlCol.nodes ('/option/grant') A(gr)
CROSS APPLY A.gr.nodes('schedules/schedule') B(sch)
)
SELECT dt.*
FROM DerivedTable dt
--use a simple WHERE here
This will return the XML's content as derived table. Use a simple WHERE to filter this derived table.
Below is the work around which worked for me.
SELECT e.XmlCol.value('(/option/OptionName)[1]', 'varchar(100)') OptionName,
t.x.value('../.././GrantName[1]','varchar(100)') GrantName,
t.x.value('(./scheduleDate)[1]', 'varchar(100)') scheduleDate
FROM #tbl e
CROSS APPLY e.XmlCol.nodes ('/option/grant/schedules/schedule') t(x)
WHERE TRY_CONVERT(datetime,NULLIF(t.x.value('(./scheduleDate)[1]','varchar(max)'), '')) BETWEEN CONVERT(date,GETDATE()) AND DATEADD(YEAR, 1, GETDATE())

Passing irregular xml file to the stored procedure

I have a sample xml as follows. I am trying to get all or specific data and then insert into the my sql table which has the same columns representing the values coming from xml. I looked through some solutions but the xml files are not formatted like I have in here. Can you help me?
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>
You can use an XML type and XML methods if you remove or modify the declaration. The SQL Server XML type only supports UCS-2 encoding and doesn't recognize "standalone". The example below uses string manipulation to tweak the declaration. You'll need to change the data types according to your actual column types and should specify an explicit column list on the INSERT statement. I omitted that in this example only because I didn't want to assume your actual table columns matched the element names in the XML.
DECLARE #xml xml;
DECLARE #xmlString nvarchar(MAX) = N'<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>';
SET #xmlString = REPLACE(#xmlString, 'encoding="UTF-8"', 'encoding="UCS-2"');
SET #xmlString = REPLACE(#xmlString, 'standalone="true"', '');
SET #xml = #xmlString;
WITH XMLNAMESPACES (
DEFAULT 'http://fms-standard.com/rfms/v1.0.0/xsd/status'
,'http://fms-standard.com/rfms/v1.0.0/xsd/common/position' AS ns2
)
INSERT INTO dbo.YourTable
SELECT
#xml.value('(/VehicleStatusResponse/VehicleStatus/VIN)[1]', 'varchar(50)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/TriggerType)[1]', 'varchar(50)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/CreatedDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/ReceivedDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Latitude)[1]', 'decimal(8,5)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Longitude)[1]', 'decimal(8,5)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Heading)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Altitude)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Speed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:PositionDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/WheelBasedSpeed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/TachographSpeed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/HRTotalVehicleDistance)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/EngineTotalFuelUsed)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/CatalystFuelLevel)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GrossCombinationVehicleWeight)[1]', 'int');
First of all you need to get your value into a declare variable of type XML or into an XML-typed data table column. As your XML contains namespaces you have to declare them in a WITH XMLNAMESPACES first. You might use wildcard syntax (*:), but its better to be as specific as possible.
The .nodes() call navigates to the Level of <VehicleStatus>. All elements below are simply 1:1 and easy to read...
You can try it like this:
DECLARE #xml XML=
N'<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>';
--This is the query
WITH XMLNAMESPACES(DEFAULT 'http://fms-standard.com/rfms/v1.0.0/xsd/status'
,'http://fms-standard.com/rfms/v1.0.0/xsd/common/position' AS ns2)
SELECT vs.value('VIN[1]','nvarchar(max)') AS VehicleStatus_VIN
,vs.value('TriggerType[1]','nvarchar(max)') AS VehicleStatus_TriggerType
,vs.value('CreatedDateTime[1]','datetime') AS VehicleStatus_CreatedDateTime
,vs.value('ReceivedDateTime[1]','datetime') AS VehicleStatus_ReceivedDateTime
,vs.value('(GNSSPosition/ns2:Latitude)[1]','decimal(14,6)') AS VehicleStatus_GNSSPosition_Latitude
,vs.value('(GNSSPosition/ns2:Longitude)[1]','decimal(14,6)') AS VehicleStatus_GNSSPosition_Longitude
/*other columns follow the same pattern*/
FROM #xml.nodes('/VehicleStatusResponse/VehicleStatus') AS A(vs)
update: insert into a table
Easiest was to wrap this call as CTE like
WITH XMLNAMESPACES(...)
,DerivedTableCTE AS
(
The query here
)
INSERT INTO YourTable (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM DerivedTableCTE

issue with xml parsing data to sql query

I am working on SQL query where i need to parse xml to sql .Query is working for other xml but not working for below xml. query i am using is as below
DECLARE #xmldata XML
SET #xmldata =
N'<SearchProductsResponse xmlns="http://api.abc.com/">
<productItems>
<productItem id="5d0ee86d84bcc5edef43236d61419a59">
<trackingLinks>
<trackingLink adspaceId="100">
<ppv>
abc.com
</ppv>
<ppc>
abc.com
</ppc>
</trackingLink>
</trackingLinks>
</productItem>
</productItems>
</SearchProductsResponse>';
select
t1.c.value('#id', 'varchar(300)') as itemid,
c.c.value('#id', 'int') as adspaceId
from
#xmldata.nodes('*:SearchProductsResponse/*:productItems/*:productItem') as t1(c)
OUTER APPLY t1.c.nodes('*:trackingLinks/*:trackingLink') as c(c)
Output I am getting is
itemid adspaceId
5d0ee86d84bcc5edef43236d61419a59 NULL
But I should return 100 instead of NULL . This query is working for other XML but don't know whats wrong with this XML. I have double check XML and query nothing different from other XML. Hope I am not missing some silly mistake
Change the attribute selection for your adspaceId column to #adspaceId.
select
t1.c.value('#id', 'varchar(300)') as itemid,
c.c.value('#adspaceId', 'int') as adspaceId
from #xmldata.nodes('*:SearchProductsResponse/*:productItems/*:productItem') as t1(c)
OUTER APPLY t1.c.nodes('*:trackingLinks/*:trackingLink') as c(c)

Stored Procedure Inserts date value one less than the actual date in the XML string

I am using XML to insert/update/delete records in table. But when I insert the row, the date value is inserted one less than the actual value. Below is the code for my Stored Procedure
IF(#Mode='UPDATE_TABLE')
BEGIN
;WITH XmlData AS
(
SELECT
NDS.DT.value('(ClaimExpenseID)[1]', 'int') AS 'ClaimExpenseID',
NDS.DT.value('(ClaimID)[1]', 'int') AS 'ClaimID',
NDS.DT.value('(POrderID)[1]', 'int') AS 'POrderID',
NDS.DT.value('(SiteID)[1]', 'int') AS 'SiteID',
NDS.DT.value('(FromDate)[1]', 'datetime') AS 'FromDate',
NDS.DT.value('(ToDate)[1]', 'datetime') AS 'ToDate',
NDS.DT.value('(ParticularID)[1]', 'int') AS 'ParticularID',
NDS.DT.value('(Description)[1]', 'varchar(200)') AS 'Description',
NDS.DT.value('(SubmittedAmount)[1]', 'int') AS 'SubmittedAmount',
NDS.DT.value('(CreatedDate)[1]', 'datetime') AS 'CreatedDate',
NDS.DT.value('(ApprovedAmount)[1]', 'int') AS 'ApprovedAmount',
NDS.DT.value('(ApprovedDate)[1]', 'datetime') AS 'ApprovedDate',
NDS.DT.value('(Remark)[1]', 'varchar(300)') AS 'Remark',
NDS.DT.value('(ApproveBy)[1]', 'int') AS 'ApproveBy',
NDS.DT.value('(RowInfo)[1]', 'varchar(20)') AS 'RowInfo'
FROM
#xmlString.nodes('/NewDataSet/DataTable') AS NDS(DT)
)
MERGE INTO dbo.ClaimExpenseTRS CET
USING XmlData x ON CET.ClaimExpenseID = x.ClaimExpenseID
WHEN MATCHED AND x.RowInfo = 'UPDATE'
THEN
UPDATE SET
CET.ClaimID=x.ClaimID,
CET.CreatedDate=x.CreatedDate,
CET.POrderID=x.POrderID,
CET.SiteID=x.SiteID,
CET.FromDate=x.FromDate,
CET.ToDate=x.ToDate,
CET.ParticularID=x.ParticularID,
CET.Description=x.Description,
CET.SubmittedAmount=x.SubmittedAmount,
CET.ApprovedAmount=x.ApprovedAmount,
CET.Remarks=x.Remark,
CET.ApproveBy=x.ApproveBy,
CET.ApprovedDate=x.ApprovedDate
WHEN MATCHED AND x.RowInfo = 'DELETE'AND CET.ClaimExpenseID = x.ClaimExpenseID
THEN DELETE
WHEN NOT MATCHED AND x.RowInfo = 'NEW'
THEN
INSERT(ClaimID, CreatedDate, POrderID, SiteID,FromDate,ToDate,ParticularID, Description,SubmittedAmount,ApprovedAmount,Remarks,ApproveBy,ApprovedDate)
VALUES(x.ClaimID,x.CreatedDate,x.POrderID,x.SiteID,x.FromDate,x.ToDate,x.
ParticularID,x.Description,x.SubmittedAmount,x.ApprovedAmount,x.Remark,x. ApproveBy,x.ApprovedDate);
END
This is the XML string..
<NewDataSet>
<DataTable>
<ClaimExpenseID>5</ClaimExpenseID>
<ClaimID>1</ClaimID>
<CreatedDate>2011-08-01T00:00:00+05:30</CreatedDate>
<POrderID>11</POrderID>
<SiteID>4</SiteID>
<FromDate>2011-08-07T00:00:00+05:30</FromDate>
<ToDate>2011-08-08T00:00:00+05:30</ToDate>
<NoOfDays>1</NoOfDays>
<ParticularID>1</ParticularID>
<Description>test</Description>
<SubmittedAmount>500</SubmittedAmount>
<Month>August</Month>
<Year>2011</Year>
<POrderNo>PO0002</POrderNo>
<SiteName>SITE 2</SiteName>
<ParticulerName>Food</ParticulerName>
<RowInfo>UNCHANGED</RowInfo>
<TableRowIndex>3</TableRowIndex>
</DataTable>
<DataTable>
<ClaimID>1</ClaimID>
<CreatedDate>2011-09-22T00:00:00+05:30</CreatedDate>
<POrderID>26</POrderID>
<SiteID>1</SiteID>
<FromDate>2011-09-22T00:00:00+05:30</FromDate>
<ToDate>2011-09-30T00:00:00+05:30</ToDate>
<NoOfDays>8</NoOfDays>
<ParticularID>1</ParticularID>
<Description>dinner</Description>
<SubmittedAmount>200</SubmittedAmount>
<POrderNo>PO-01</POrderNo>
<SiteName>ALKAPURI</SiteName>
<ParticulerName>Food</ParticulerName>
<RowInfo>NEW</RowInfo>
<TableRowIndex>4</TableRowIndex>
</DataTable>
</NewDataSet>
In second data table the from date value is 2011-09-22 and To date value is 2011-09-30 but when the value inserted in the database table it becomes 2011-09-21 and 2011-09-29 respectively..
Could this be your +5:30 timezone? Is this the same as your database or is your database trying to work out the time in a different timezone and coming up with the previous day?
Edit: I just confirmed this. If I read your date time: 2011-09-30T00:00:00+05:30 into my machine (based in the UK), I get a return of 2011-09-29 18:30:00.000
The code I used for the conversion was :
select cast('' as xml).value('xs:dateTime("2011-09-30T00:00:00+05:30")', 'datetime')