I am trying to parse a simple XML file. As soon as I un comment the insert statement it gives me invalid column errors.
drop table #TEMP
drop table #TEMP_T
declare #XMl_DATA AS XML
set #XMl_DATA =
'<DocumentElement>
<Att_Table>
<L_ATTR_CD>GAS_FLOW_START_DATE</L_ATTR_CD>
<L_ATTR_DESC>GAS FLOW START DATE</L_ATTR_DESC>
<L_ATTR_VALUE>01/01/2012</L_ATTR_VALUE>
<R_ATTR_CD>EX_CTRCT_CO_ID</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE GID(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>GAS_FLOW_END_DATE</L_ATTR_CD>
<L_ATTR_DESC>GAS FLOW END DATE</L_ATTR_DESC>
<L_ATTR_VALUE>01/31/2012</L_ATTR_VALUE>
<R_ATTR_CD>EX_CTRCT_NBR</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE CONTRACT NUMBER(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>CTRCT_CO_ID</L_ATTR_CD>
<L_ATTR_DESC>GID(S)</L_ATTR_DESC>
<L_ATTR_VALUE />
<R_ATTR_CD>EX_RATE_CMPNT_CD</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE RATE COMPONENT CODE(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>CTRCT_NBR</L_ATTR_CD>
<L_ATTR_DESC>DART STYLE CONTRACT NUMBER(S)</L_ATTR_DESC>
<L_ATTR_VALUE />
<R_ATTR_CD>EX_PT_ID_NBR</R_ATTR_CD>
<R_ATTR_DESC>EXCLIDE POINT ID NUMBER(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
</DocumentElement>'
Temp table:
CREATE TABLE #TEMP_T
(
ID INT IDENTITY(1,1),
ATT_CD VARCHAR(50),
ATT_CD_VALUE VARCHAR(1000)
)
SELECT
cast(Colx.query('data(L_ATTR_CD)') as varchar(max))as L_ATTR_CD,
cast(Colx.query('data(L_ATTR_VALUE)') as varchar(max))as L_ATTR_CD_VALUE,
cast(Colx.query('data(R_ATTR_CD)') as varchar(max)) as R_ATTR_CD,
cast(Colx.query('data(R_ATTR_VALUE)') as varchar(max))as R_ATTR_CD_VALUE
INTO #TEMP
FROM #XMl_DATA.nodes('DocumentElement/Att_Table') AS T(Colx)
--INSERT INTO #TEMP_T(ATT_CD,ATT_CD_VALUE)
--SELECT LTRIM(RTRIM(L_ATT_CD)),LTRIM(RTRIM(L_ATT_CD_VALUE))
--FROM #TEMP
--WHERE L_ATT_CD_VALUE <> 'NO_DATA'
--INSERT INTO #TEMP_T(ATT_CD,ATT_CD_VALUE)
--SELECT LTRIM(RTRIM(R_ATT_CD)),LTRIM(RTRIM(R_ATT_CD_VALUE))
--FROM #TEMP
--WHERE R_ATT_CD_VALUE <>'NO_DATA'
Output:
select * from #TEMP_T
select * from #TEMP
Sometimes you use things like L_ATTR and R_ATTR, and other times you use things like L_ATT and R_ATT (with no Rs). Pick one and stick to it.
The error message mentioning "invalid column" was trying to tell you this: the columns you try to select from #TEMP are "invalid" because you aren't using the same names as when you created #TEMP.
Why don't you just do something like this?
-- define your XML structure and create the #TEMP_T table
;WITH ParsedData AS
(
SELECT
Colx.value('(L_ATTR_CD)[1]', 'varchar(50)') as L_ATTR_CD,
Colx.value('(L_ATTR_VALUE)[1]', 'varchar(50)') as L_ATTR_CD_VALUE,
Colx.value('(R_ATTR_CD)[1]', 'varchar(50)') as R_ATTR_CD,
Colx.value('(R_ATTR_VALUE)[1]', 'varchar(50)') as R_ATTR_CD_VALUE
FROM
#XMl_DATA.nodes('DocumentElement/Att_Table') AS T(Colx)
)
INSERT INTO #temp_T(ATT_CD, ATT_CD_VALUE)
SELECT L_ATTR_CD, L_ATTR_CD_VALUE
FROM parseddata
UNION
SELECT R_ATTR_CD, R_ATTR_CD_VALUE
FROM parseddata
This just parses the XML (much simpler than your approach, too!) and then inserts both the (L_ATTR_CD, L_ATTR_CD_VALUE) as well as the (R_ATTR_CD, R_ATTR_CD_VALUE) pairs into the temp table in a single go.
Related
I wanted the output of my xml to look like this:
<CustomConfigurationSteps>
<CustomConfigurations>
<CustomConfiguration SiteURL="abc">
<CustomElements>
<CustomElement Type="Searchtype" unit="3" price="5">
<ClassValue>AZSupreme</ClassValue>
</CustomElement>
</CustomElements>
<CustomElements>
<CustomElement Type="Searchtype" unit="3" price="5">
<ClassVaue>AZSupreme</ClassVaue>
</CustomElement>
</CustomElements>
</CustomConfiguration>
</CustomConfigurations>
</CustomConfigurationSteps>
I tried the below logic, but its not giving me the exact output.
DECLARE #xml xml
SET #xml = (SELECT
config.SiteURL AS '#SiteURL',
(SELECT
CustomConfiguration.Type AS '#Type',
CustomConfiguration.[PackageName] AS '#unit',
CustomConfiguration.[UseExecute] AS '#price',
ClassValue
FOR XML PATH('CustomElement'), TYPE) AS CustomElements
FROM
[dbo].[CustomConfigurationSteps] AS CustomConfiguration WITH (nolock)
INNER JOIN
WebSiteConfiguration AS config WITH (nolock) ON config.ConfigurationID = CustomConfiguration.[ConfigurationID]
WHERE
CustomConfiguration.[ConfigurationID] = 9
FOR XML path ('CustomConfiguration'), TYPE)
--set #xml1=( SELECT #xml FOR XML path ('CustomConfiguration') ,type)
SELECT #xml
FOR XML path ('CustomConfigurations'), ROOT ('CustomConfigurationSteps')
Try it like this (although this looks a little odd, especially the repeating <CustomElements> node.
A mockup table to simulate your issue:
DECLARE #tbl TABLE([Type] VARCHAR(100),unit int, price DECIMAL(10,4),ClassValue VARCHAR(100));
INSERT INTO #tbl VALUES('Searchtype 1',1,11,'AZSupreme 1')
,('Searchtype 2',2,22,'AZSupreme 2');
--The query
SELECT 'abc' AS [CustomConfiguration/#SiteURL]
,(
SELECT t.[Type] AS [CustomElement/#Type]
,t.unit AS [CustomElement/#unit]
,t.price AS [CustomElement/#pricee]
,t.ClassValue [CustomElement/ClassValue]
FROM #tbl t
FOR XML PATH('CustomElements'),TYPE
) AS CustomConfiguration
FOR XML PATH('CustomConfigurations'),ROOT('CustomConfigurationSteps');
In the T-SQL sample code below I am trying to query related pieces of data that are in different nodes in the xml, but I can't figure out how to do that. For example, the LX01_AssignedNumber and the C00302_ProcedureCode values need to be pulled together for the same record. The result should look like this.
CLAIM_SOURCE_ID ITEM_NUMBER HCPCS_LINE_CODE
16202E123456 1 99203
16202E123456 2 96372
Can someone help me?
USE [tempdb];
GO
DECLARE #XML XML =
N'<ns1:X12EnrichedMessage xmlns:ns1="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML">
<TransactionSet>
<!-- ProcessLogID=PLG0007182226 ;ProcessLogDetailID=PLG0007182968 ;EnvID=1;RetryCount=1 -->
<ns0:X12_00501_837_P xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
<ns0:TS837_2000A_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
<ns0:TS837_2000B_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
<ns0:TS837_2300_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
<ns0:TS837_2400_Loop>
<ns0:LX_ServiceLineNumber>
<LX01_AssignedNumber>1</LX01_AssignedNumber>
</ns0:LX_ServiceLineNumber>
<ns0:SV1_ProfessionalService>
<ns0:C003_CompositeMedicalProcedureIdentifier>
<C00301_ProductorServiceIDQualifier>HC</C00301_ProductorServiceIDQualifier>
<C00302_ProcedureCode>99203</C00302_ProcedureCode>
<C00303_ProcedureModifier>25</C00303_ProcedureModifier>
<C00307_Description>NO DESCRIPTION</C00307_Description>
</ns0:C003_CompositeMedicalProcedureIdentifier>
<SV102_LineItemChargeAmount>167.82</SV102_LineItemChargeAmount>
<SV103_UnitorBasisforMeasurementCode>UN</SV103_UnitorBasisforMeasurementCode>
<SV104_ServiceUnitCount>1</SV104_ServiceUnitCount>
<ns0:C004_CompositeDiagnosisCodePointer>
<C00401_DiagnosisCodePointer>1</C00401_DiagnosisCodePointer>
</ns0:C004_CompositeDiagnosisCodePointer>
</ns0:SV1_ProfessionalService>
<ns0:DTP_SubLoop_2>
<ns0:DTP_Date_ServiceDate>
<DTP01_DateTimeQualifier>472</DTP01_DateTimeQualifier>
<DTP02_DateTimePeriodFormatQualifier>RD8</DTP02_DateTimePeriodFormatQualifier>
<DTP03_ServiceDate>20160627-20160627</DTP03_ServiceDate>
</ns0:DTP_Date_ServiceDate>
</ns0:DTP_SubLoop_2>
</ns0:TS837_2400_Loop>
<ns0:TS837_2400_Loop>
<ns0:LX_ServiceLineNumber>
<LX01_AssignedNumber>2</LX01_AssignedNumber>
</ns0:LX_ServiceLineNumber>
<ns0:SV1_ProfessionalService>
<ns0:C003_CompositeMedicalProcedureIdentifier>
<C00301_ProductorServiceIDQualifier>HC</C00301_ProductorServiceIDQualifier>
<C00302_ProcedureCode>96372</C00302_ProcedureCode>
<C00307_Description>NO DESCRIPTION</C00307_Description>
</ns0:C003_CompositeMedicalProcedureIdentifier>
<SV102_LineItemChargeAmount>82.56</SV102_LineItemChargeAmount>
<SV103_UnitorBasisforMeasurementCode>UN</SV103_UnitorBasisforMeasurementCode>
<SV104_ServiceUnitCount>2</SV104_ServiceUnitCount>
<ns0:C004_CompositeDiagnosisCodePointer>
<C00401_DiagnosisCodePointer>2</C00401_DiagnosisCodePointer>
</ns0:C004_CompositeDiagnosisCodePointer>
</ns0:SV1_ProfessionalService>
<ns0:DTP_SubLoop_2>
<ns0:DTP_Date_ServiceDate>
<DTP01_DateTimeQualifier>472</DTP01_DateTimeQualifier>
<DTP02_DateTimePeriodFormatQualifier>RD8</DTP02_DateTimePeriodFormatQualifier>
<DTP03_ServiceDate>20160627-20160627</DTP03_ServiceDate>
</ns0:DTP_Date_ServiceDate>
</ns0:DTP_SubLoop_2>
</ns0:TS837_2400_Loop>
</ns0:TS837_2300_Loop>
</ns0:TS837_2000B_Loop>
</ns0:TS837_2000A_Loop>
</ns0:X12_00501_837_P>
</TransactionSet>
</ns1:X12EnrichedMessage>'
IF OBJECT_ID(N'tempdb..#CLAIM_XML', N'U') IS NOT NULL
DROP TABLE #CLAIM_XML;
CREATE TABLE #CLAIM_XML (
CLAIM_SOURCE_ID VARCHAR(20) NOT NULL
,RAW_XML XML NOT NULL
,CLAIM_FORM_TYPE CHAR(1) NOT NULL
,CREATED_DATE DATE NOT NULL
,CONSTRAINT CLAIM_XML_PK PRIMARY KEY (CLAIM_SOURCE_ID)
);
CREATE PRIMARY XML INDEX CLAIM_XML_RAW_XML_IDX
ON #CLAIM_XML (RAW_XML);
INSERT INTO #CLAIM_XML
([CLAIM_SOURCE_ID]
,[RAW_XML]
,[CLAIM_FORM_TYPE]
,[CREATED_DATE])
VALUES('16202E123456'
,#XML
,'H'
,CONVERT(DATE, DATEADD(DAY, -1, GETDATE())));
WITH XMLNAMESPACES ('http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML' AS ns1
,'http://schemas.microsoft.com/BizTalk/EDI/X12/2006' AS ns0)
SELECT [CX].[CLAIM_SOURCE_ID]
,[ITEM_NUMBER] = LineNumber.ref.value('text()[1]', 'int')
,[HCPCS_LINE_CODE] = [CX].[RAW_XML].value('(/ns1:X12EnrichedMessage/TransactionSet/ns0:X12_00501_837_P/ns0:TS837_2000A_Loop/ns0:TS837_2000B_Loop/ns0:TS837_2300_Loop/ns0:TS837_2400_Loop/ns0:SV1_ProfessionalService/ns0:C003_CompositeMedicalProcedureIdentifier/C00302_ProcedureCode)[1]','varchar(100)')
FROM #CLAIM_XML AS [CX]
CROSS APPLY [CX].[RAW_XML].nodes('/ns1:X12EnrichedMessage/TransactionSet/ns0:X12_00501_837_P/ns0:TS837_2000A_Loop/ns0:TS837_2000B_Loop/ns0:TS837_2300_Loop/ns0:TS837_2400_Loop/ns0:LX_ServiceLineNumber/*') LineNumber(ref)
WHERE [CX].[CLAIM_FORM_TYPE] = 'H'
AND [CX].[CREATED_DATE] = CONVERT(DATE, DATEADD(DAY, -1, GETDATE()));
Use multiple CROSS APPLYs to access the different parts of the XML, like this:
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML' AS ns1
,'http://schemas.microsoft.com/BizTalk/EDI/X12/2006' AS ns0)
SELECT
c.[CLAIM_SOURCE_ID],
sln.c.value('(LX01_AssignedNumber/text())[1]', 'INT') AS [ITEM_NUMBER],
ps.c.value('(ns0:C003_CompositeMedicalProcedureIdentifier/C00302_ProcedureCode/text())[1]', 'INT') AS [HCPCS_LINE_CODE]
FROM #CLAIM_XML c
CROSS APPLY c.[RAW_XML].nodes('/ns1:X12EnrichedMessage/TransactionSet/ns0:X12_00501_837_P/ns0:TS837_2000A_Loop/ns0:TS837_2000B_Loop/ns0:TS837_2300_Loop/ns0:TS837_2400_Loop') l(c)
CROSS APPLY l.c.nodes('ns0:LX_ServiceLineNumber') sln(c)
CROSS APPLY l.c.nodes('ns0:SV1_ProfessionalService') ps(c)
No need for multiple CROSS APPLY with .nodes(). As the values you want to read are single occurance within their tree you can address them directly:
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML' AS ns1
,'http://schemas.microsoft.com/BizTalk/EDI/X12/2006' AS ns0)
SELECT
loop2400.value('(ns0:LX_ServiceLineNumber/LX01_AssignedNumber)[1]', 'INT') AS [ITEM_NUMBER],
loop2400.value('(ns0:SV1_ProfessionalService/ns0:C003_CompositeMedicalProcedureIdentifier/C00302_ProcedureCode)[1]', 'INT') AS [HCPCS_LINE_CODE]
FROM #xml.nodes('/ns1:X12EnrichedMessage/TransactionSet/ns0:X12_00501_837_P/ns0:TS837_2000A_Loop/ns0:TS837_2000B_Loop/ns0:TS837_2300_Loop/ns0:TS837_2400_Loop') A(loop2400)
The lazy approach works too, but - in general - it's good advise to be as specific as possible...
SELECT
loop2400.value('(*//LX01_AssignedNumber)[1]', 'INT') AS [ITEM_NUMBER],
loop2400.value('(*//C00302_ProcedureCode)[1]', 'INT') AS [HCPCS_LINE_CODE]
FROM #xml.nodes('//*:TS837_2400_Loop') A(loop2400)
I have an ID table. Right now I just need to generate a XML file as the following format. Actually, it is a list of integers.
--Target format
<ListOfIntegers>
<Integer>150356</Integer>
<Integer>150365</Integer>
</ListOfIntegers>
I tried to use this and many other ways,
SELECT #tempXML = ( SELECT ID AS [Integer]
FROM #tempIDTable
FOR XML PATH('ListOfIntegers'), ELEMENTS)
But it will only generate a XML file like
--Current format
<ListOfIntegers>
<Integer>150356</Integer>
</ListOfIntegers>
<ListOfIntegers>
<Integer>150365</Integer>
</ListOfIntegers>
What should I do?
You were quite close:
CREATE TABLE #tempIDTable(ID INT);
INSERT INTO #tempIDTable VALUES(1),(2),(3);
DECLARE #tempXML XML;
SELECT #tempXML = ( SELECT ID AS [Integer]
FROM #tempIDTable
FOR XML PATH(''),ROOT('ListOfIntegers'));
SELECT #tempXML;
DROP TABLE #tempIDTable;
The result:
<ListOfIntegers>
<Integer>1</Integer>
<Integer>2</Integer>
<Integer>3</Integer>
</ListOfIntegers>
I am passing string values from my code like '12th Standard/Ordinary National Diploma,Higher National Diploma' to SQL query, but I am not getting any values and nothing showing any result.
My SQL query:
declare #qua varchar(250),#final varchar(250),#Qualification varchar(250)
set #Qualification= '12th Standard/Ordinary National Diploma,Higher National Diploma'
set #qua =replace(#Qualification,',',''',''')
set #final= ''''+#qua+''''
select * from mytablename in(#final)
Result: Data is not displaying
Thank you in advance.
Instead do it using a table variable like
declare #tbl table(qual varchar(250));
insert into #tbl
select '12th Standard/Ordinary National Diploma'
union
select 'Higher National Diploma';
select * from mytablename where somecolumn in(select qual from #tbl);
Despite trying to put quote marks in there, you're still only passing a single string to the IN. The string just contains embedded quotes and SQL Server is looking for that single long string.
You also don't seem to be comparing a column for the IN.
Your best bet is to pass in multiple string variables, but if that's not possible then you'll have to write a function that parses a single string into a resultset and use that. For example:
SELECT
Column1, -- Because we never use SELECT *
Column2
FROM
MyTableName
WHERE
qualification IN (SELECT qualification FROM dbo.fn_ParseString(#qualifications))
You can insert all your search criteria in one table and then can easily do a lookup on the main table, example below:
DECLARE #MyTable TABLE (Name VARCHAR(10), Qualification VARCHAR(50))
DECLARE #Search TABLE (Qualifications VARCHAR(50))
INSERT INTO #MyTable VALUES ('User1','12th Standard'), ('User2','Some Education'),
('User3','Ordinary National Diploma'), ('User4','Some Degree'),
('User5','Higher National Diploma')
INSERT INTO #Search VALUES ('12th Standard'),('Ordinary National Diploma'),('Higher National Diploma')
SELECT MT.*
FROM #MyTable MT
INNER JOIN (SELECT Qualifications FROM #Search) S ON S.Qualifications = MT.Qualification
As previous said, you are passing a string with commas, not comma separated values. It needs to be split up into separate values.
You can do this by passing the qualification string into XML which you can use to turn it into separate rows of data.
The IN parameter will then accept the data as separate values.
DECLARE #Qualifications as varchar(150) = '12th Standard/Ordinary National Diploma,Higher National Diploma'
Declare #Xml XML;
SET #Xml = N'<root><r>' + replace(#Qualifications, char(44),'</r><r>') + '</r></root>';
select *
from MyTableName
Where MyTableName.Qualification in
(select r.value('.','varchar(max)') as item
from #Xml.nodes('//root/r') as records(r))
Alternatively you can create a table-valued function that splits according to input like in your case its ',' and then INNER JOIN with the returnColumnname and that particular column that you want to filter
SELECT COLUMNS, . . . .
FROM MyTableName mtn
INNER JOIN dbo.FNASplitToTable(#qualifications, ',') csvTable
ON csvTable.returnColumnName = mtn.somecolumn
Table Valued function might be like:
CREATE FUNCTION dbo.FNASplitToTable (#string varchar(MAX), #splitType CHAR(1))
RETURNS #result TABLE(Value VARCHAR(100))
AS
BEGIN
DECLARE #x XML
SELECT #x = CAST('<A>' + REPLACE(#string, #splitType, '</A><A>') + '</A>' AS XML)
INSERT INTO #result
SELECT LTRIM(t.value('.', 'VARCHAR(100)')) AS inVal
FROM #x.nodes('/A') AS x(t)
RETURN
END
GO
I have table which contains columns like
SiteID (identity_Col)
SiteName
POrderID
Location
Address
Cluster
VenderName
I want to use xml string to insert/update/delete data in this table. Apart from this column, XML string contains one more column viz. RowInfo. This column will have values like "Unchanged","Update","New","Delete". Based on this values the rows in the table should be inserted,updated,deleted.
My XML string is as below:
<NewDataSet>
<DataTable>
<SiteID>2</SiteID>
<SiteName>NIZAMPURA</SiteName>
<POrderID>7</POrderID>
<Location>NIZAMPURA</Location>
<SiteAddress>Vadodara</SiteAddress>
<Cluster>002</Cluster>
<SubVendorName>Test Vender-1</SubVendorName>
<RowInfo>UNCHANGED</RowInfo>
</DataTable>
<DataTable>
<SiteID>16</SiteID>
<SiteName>Site-1</SiteName>
<POrderID>7</POrderID>
<Location>Alkapuri</Location>
<SiteAddress>test</SiteAddress>
<Cluster>Test Cluster</Cluster>
<SubVendorName>Test Vender12</SubVendorName>
<RowInfo>UNCHANGED</RowInfo>
</DataTable>
<DataTable>
<SiteID>17</SiteID>
<SiteName>Site-3</SiteName>
<POrderID>7</POrderID>
<Location>Alkapuri123</Location>
<SiteAddress>test123</SiteAddress>
<Cluster>Test Cluster123</Cluster>
<SubVendorName>Test Vender123</SubVendorName>
<RowInfo>DELETE</RowInfo>
</DataTable>
</NewDataSet>'
This is the code that I have written to insert data in table if RowInfo = "NEW"
IF len(ISNULL(#xmlString, '')) > 0
BEGIN
DECLARE #docHandle1 int = 0;
EXEC sp_xml_preparedocument #docHandle1 OUTPUT, #xmlString
INSERT INTO [SiteTRS] (
[SiteName],
[POrderID],
[Location],
[SiteAddress],
[Cluster],
[SubVendorName])
SELECT SiteName,POrderID,Location,SiteAddress,Cluster,SubVendorName
FROM OPENXML (#docHandle1, '/NewDataSet/DataTable')
WITH (SiteName varchar(50) './SiteName',
POrderID varchar(50) './PorderID',
Location varchar(50) './Location',
SiteAddress varchar(max) './SiteAddress',
Cluster varchar(50) './Cluster',
SubVendorName varchar(50) './SubVendorName',
RowInfo varchar(30) './RowInfo')
WHERE RowInfo='NEW'
But I don't know how to use XML to update/delete records in the table. Please guide
I am novice in the XML so dont have any idea. Please forgive me if I am making something childish.
I would strongly recommend not to use the old, legacy OPENXML stuff anymore - with XML support in SQL Server, it's much easier to use the built-in XPath/XQuery methods.
In your case, I would use a CTE (Common Table Expression) to break up the XML into an "inline" table of rows and columns:
DECLARE #input XML = '<NewDataSet>
<DataTable>
<SiteID>2</SiteID>
<SiteName>NIZAMPURA</SiteName>
<POrderID>7</POrderID>
<Location>NIZAMPURA</Location>
<SiteAddress>Vadodara</SiteAddress>
<Cluster>002</Cluster>
<SubVendorName>Vender-1</SubVendorName>
<RowInfo>UPDATE</RowInfo>
</DataTable>
<DataTable>
<SiteName>Site-1</SiteName>
<POrderID>7</POrderID>
<Location>Alkapuri</Location>
<SiteAddress>test</SiteAddress>
<Cluster>Cluster-1</Cluster>
<SubVendorName>Test Vender</SubVendorName>
<RowInfo>NEW</RowInfo>
</DataTable>
</NewDataSet>'
;WITH XMLData AS
(
SELECT
NDS.DT.value('(SiteID)[1]', 'int') AS 'SiteID',
NDS.DT.value('(SiteName)[1]', 'varchar(50)') AS 'SiteName',
NDS.DT.value('(POrderID)[1]', 'int') AS 'POrderID',
NDS.DT.value('(Location)[1]', 'varchar(100)') AS 'Location',
NDS.DT.value('(SiteAddress)[1]', 'varchar(100)') AS 'SiteAddress',
NDS.DT.value('(Cluster)[1]', 'varchar(100)') AS 'Cluster',
NDS.DT.value('(SubVendorName)[1]', 'varchar(100)') AS 'SubVendorName',
NDS.DT.value('(RowInfo)[1]', 'varchar(20)') AS 'RowInfo'
FROM
#input.nodes('/NewDataSet/DataTable') AS NDS(DT)
)
SELECT *
FROM XMLDATA
This gives you rows and columns which you can work with.
SiteID SiteName POrderID Location SiteAddress Cluster SubVendorName RowInfo
2 NIZAMPURA 7 NIZAMPURA Vadodara 002 Vender-1 UPDATE
NULL Site-1 7 Alkapuri test Cluster-1 Test Vender NEW
Now if you're on SQL Server 2008 or newer, you could combine this with the MERGE command to do your INSERT/UPDATE in a single statement, basically.
If you're on 2005, you will need to either store this information into a temporary table / table variable inside your stored proc, or you need to do the select multiple times; the CTE allows only one single command to follow it.
Update: with this CTE, you can then combine it with a MERGE:
;WITH XmlData AS
(
SELECT
NDS.DT.value('(SiteID)[1]', 'int') AS 'SiteID',
NDS.DT.value('(SiteName)[1]', 'varchar(50)') AS 'SiteName',
NDS.DT.value('(POrderID)[1]', 'int') AS 'POrderID',
NDS.DT.value('(Location)[1]', 'varchar(100)') AS 'Location',
NDS.DT.value('(SiteAddress)[1]', 'varchar(100)') AS 'SiteAddress',
NDS.DT.value('(Cluster)[1]', 'varchar(100)') AS 'Cluster',
NDS.DT.value('(SubVendorName)[1]', 'varchar(100)') AS 'SubVendorName',
NDS.DT.value('(RowInfo)[1]', 'varchar(20)') AS 'RowInfo'
FROM
#input.nodes('/NewDataSet/DataTable') AS NDS(DT)
)
MERGE INTO dbo.SiteTRS t
USING XmlData x ON t.SiteID = x.SiteID
WHEN MATCHED AND x.RowInfo = 'UPDATE'
THEN
UPDATE SET
t.SiteName = x.SiteName,
t.POrderID = x.POrderID,
t.Location = x.Location,
t.SiteAddress = x.SiteAddress,
t.Cluster = x.Cluster,
t.SubVendorName = x.SubVendorName
WHEN MATCHED AND x.RowInfo = 'DELETE'
THEN DELETE
WHEN NOT MATCHED AND x.RowInfo = 'NEW'
THEN
INSERT(SiteID, SiteName, POrderID, Location, SiteAddress, Cluster, SubVendorName)
VALUES(x.SiteID, x.SiteName, x.POrderID, x.Location, x.SiteAddress, x.Cluster, x.SubVendorName)
;
See some more resources:
SQL SERVER – 2008 – Introduction to Merge Statement – One Statement for INSERT, UPDATE, DELETE
Using SQL Server 2008's MERGE statement