I am trying to parse through an XML in SQL Server 2008 R2 and I am having some issues. I am trying to parse through the items for each parent node, but I'm not getting the solution The XML data is below:
<MerchantInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Active>true</Active>
<CreatedDate>2015-04-16T00:00:00+05:30</CreatedDate>
<CreatedBy>63747</CreatedBy>
<ModifiedBy>63747</ModifiedBy>
<ModifiedUserName>charucsrawat#hpcl</ModifiedUserName>
<MerchantCode>0</MerchantCode>
<RetailOutletName>Gayatri Automobiles</RetailOutletName>
<DealerName>Vandana Singh</DealerName>
<ERPCode>16622710</ERPCode>
<OwnerID>0</OwnerID>
<ICICIAcDetailsRequired>false</ICICIAcDetailsRequired>
<SupplyLocationCode>Mirzapur</SupplyLocationCode>
<IsLive>false</IsLive>
<LiveSAM>1</LiveSAM>
<TestSAM>0</TestSAM>
<OutletCategory>8001</OutletCategory>
<HighwayNo>NH7</HighwayNo>
<HighwayName>Mirzapur Rewa Road</HighwayName>
<SecurityDeposit>10000</SecurityDeposit>
<HSDSaleMonthly>200</HSDSaleMonthly>
<Comments />
<VerifiedDate>0001-01-01T00:00:00</VerifiedDate>
<VerifiedBy>0</VerifiedBy>
<isCloned>false</isCloned>
<VerifiedByUserName />
<ApprovedDate>0001-01-01T00:00:00</ApprovedDate>
<ApprovedBy>0</ApprovedBy>
</MerchantInfo>
my code is :
;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' AS mi)
SELECT
T.C.value('mi:erpcode[1]','numeric') as erpcode,
T.C.value('mi:SecurityDeposit[1]','varchar(50)') AS securitydeposit
FROM ChangeEvent ce
CROSS APPLY changeddata.nodes('mi/erpcode[1]') AS T(C)
WHERE Ce.EntityTypeId = 2
AND CAST(Ce.ChangedData AS VARCHAR(MAX)) LIKE '%16622710%'
GO
What I am looking for is:
erpcode securitydeposit
16622710 10000
Kindly help
You don't need ;WITH XMLNAMESPACES for this purpose since XML elements involved in the query doesn't use any namespace prefix and the XML document doesn't have defult namespace too.
Also note that XML element/attribute name are case-sensitive (f.e erpcode != ERPCode) :
SELECT
T.C.value('ERPCode[1]','numeric') as erpcode,
T.C.value('SecurityDeposit[1]','varchar(50)') AS securitydeposit
FROM ChangeEvent ce
CROSS APPLY ChangedData.nodes('MerchantInfo') AS T(C)
WHERE Ce.EntityTypeId = 2
AND T.C.value('ERPCode[1]','numeric') = 16622710
Demo
Related
I want to make query xml value sql server and get auditingCompanyAddress value
create table sqm (data xml)
insert into sqm
select '<taxComplianceReport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.gsis.gr/cpaaudit/v12">
<auditingGeneralInformation>
<auditingHeader>
<auditingCompany>
<auditingCompanyTitle>ΣΥΝΕΡΓΑΖΟΜΕΝΟΙ ΟΡΚΩΤΟΙ ΛΟΓΙΣΤΕΣ Α.Ε. </auditingCompanyTitle>
<auditingCompanyRegisterNum>125</auditingCompanyRegisterNum>
<auditingCompanyVatNumber>094394659</auditingCompanyVatNumber>
<auditingCompanyAddress>Φ. ΝΕΓΡΗ 3, 11257 ΑΘΗΝΑ</auditingCompanyAddress>
<auditingCompanyFee>4000</auditingCompanyFee>
</auditingCompany>
<certifiedAccountant1>
<certifiedAccountantsName>ΚΑΛΛΕΣ ΝΙΚΟΛΑΟΣ</certifiedAccountantsName>
<certifiedAccountantsRegisterNum>1590</certifiedAccountantsRegisterNum>
<accountantVatNumber>035209342</accountantVatNumber>
<certifiedAccountantsCity />
<certifiedAccountantsFee>0</certifiedAccountantsFee>
</certifiedAccountant1>
<disclaimer>true</disclaimer>
<companyName>ΣΑΛΑΓΙΑΝΝΗΣ Γ.ΑΒΕΕ</companyName>
<companyVatNumber>094357246</companyVatNumber>
<periodFrom>2018-01-01</periodFrom>
<periodTo>2018-12-31</periodTo>
<fiscalYear>2018</fiscalYear>
<conclusionReportVatCompliance>1</conclusionReportVatCompliance>
<nonImportantDiffReportVatCompliance>2</nonImportantDiffReportVatCompliance>
<pendingQuestions>false</pendingQuestions>
<fiscalSubjectsNotAuditedDueToVatProblems>
<exists>false</exists>
<comments />
</fiscalSubjectsNotAuditedDueToVatProblems>
</auditingHeader>
</auditingGeneralInformation>
</taxComplianceReport>'
I am try to get with the following sql query:
select
m.c.value('(auditingCompanyAddress)[1]', 'VARCHAR(max)') as auditingCompanyAddress
from sqm as s
outer apply s.data.nodes('taxComplianceReport/auditingGeneralInformation/auditingHeader/auditingCompany') as m(c)
but returns null. I think the problem is the taxComplianceReport but I dont know how to resolve.
Any idea?
There's a namespace in your XML yet you don't define it in your SQL. Define the DEFAULT one and it works:
WITH XMLNAMESPACES(DEFAULT'http://www.gsis.gr/cpaaudit/v12')
SELECT m.c.value('(auditingCompanyAddress)[1]', 'VARCHAR(max)') as auditingCompanyAddress
FROM sqm AS s
OUTER APPLY s.data.nodes('taxComplianceReport/auditingGeneralInformation/auditingHeader/auditingCompany') AS m(c);
Note that, for me, this returns the varchar value 'F. ??G?? 3, 11257 ?T???' as (at least on my collation) a varchar cannot contain characters like Λ and Σ. If you get ?s as well, ensure you are using nvarchars.
I am trying to run a query against some xml in SQL Server 2008 and I am not getting a result. I have done some online research and came up with the following query. I played around and was able to return the root node, but I need the values from inside and there is an array of call nodes and I need values from it.
WITH XMLNAMESPACES ('http://api.myapi.com/resource' as r)
select c.value('#AgentCall','varchar(max)') as value
from mytable cl
outer apply cl.callinfoxml.nodes('//Call') as q(c)
Sample XML:
<r:ResourceList xmlns:r="http://api.myapi.com/resource" xmlns="http://api.myapi.com/data" totalResults="1">
<Call id="1123570170003">
<FromNumber>14062618272</FromNumber>
<ToNumber>14062618272</ToNumber>
<State>READY</State>
<BatchId>12827094003</BatchId>
<BroadcastId>14633834003</BroadcastId>
<ContactId>818582749003</ContactId>
<Inbound>false</Inbound>
<Created>2016-09-22T06:22:18Z</Created>
<Modified>2016-09-22T06:22:18Z</Modified>
<AgentCall>false</AgentCall>
</Call>
</r:ResourceList>
You're not paying attention to the default XML namespace in your document - you need to reference that as well. Try this code:
WITH XMLNAMESPACES (DEFAULT 'http://api.myapi.com/data',
'http://api.myapi.com/resource' as r)
SELECT
c.value('#AgentCall', 'varchar(max)') AS value
FROM
mytable cl
OUTER APPLY
cl.callinfoxml.nodes('/r:ResourceList/Call') as q(c)
I google a lot and got no luck.
I can't retrieve data from XML column which data came from web service using sp_OAGetProperty.
the XML Column contain..
<ArrayOfCustomerInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Customer CustCode="001">
<CustName>John</CustName>
<Queues>
<Q>
<No>10</No>
<Line>1</Line>
</Q>
</Queues>
</Customer>
</ArrayOfCustomerInfo>
I got NULL when I execute following statement
(but works fine if I remove all XML namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/")
SELECT a.b.value('#CustCode','varchar(4)') AS Code
,a.b.value('CustName[1]','varchar(20)') AS Name
,c.d.value('No[1]','int') AS QNo
,c.d.value('(Line)[1]','int') AS QLine
FROM PGHRMS_Employees x
CROSS APPLY x.data.nodes('/ArrayOfCustomerInfo/Customer') AS a(b)
CROSS APPLY a.b.nodes('Queues/Q') AS c(d)
please give me some advice. I've to achieve with SQL SERVER :(
If anyone want to reproduce it, I pasted script at : http://pastebin.com/ueZGidyL
Thank you in advance !!!
Try this:
;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/')
SELECT
Code = XC1.value('#CustCode', 'varchar(4)'),
Name = XC1.value('CustName[1]', 'varchar(20)'),
QNo = XC2.value('No[1]', 'int') ,
QLine = XC2.value('(Line)[1]','int')
FROM
PGHRMS_Employees
CROSS APPLY
XmlContent.nodes('/ArrayOfCustomerInfo/Customer') AS XT1(XC1)
CROSS APPLY
XC1.nodes('Queues/Q') AS XT2(XC2)
With the WITH XMLNAMESPACES construct, you can define some XML namespaces to be used by the following T-SQL statement - default or prefixed namespaces alike.
I am new to XML and SQL Server and am trying import an XML file into SQL Server 2010. I have 14 tables that I would like to parse the data into. All 14 table names are listed in the XML as nodes (I think) I found some example code that worked with the simple example XML, but my XML seems a little more complicated and may not be structured optimally; unfortunately, I can't change that. As a basic attempt, I tried to insert the data into just one field of one existing table (SILVX_SN16000), but the Message pane shows "(0 rows(s) affected). Thanks in advance for looking at this.
USE TEST
Declare #xml XML
Select #xml =
CONVERT(XML,bulkcolumn,2) FROM OPENROWSET(BULK 'C:\Users\Kevin_S\Documents \SilvxInSightImport.xml',SINGLE_BLOB) AS X
SET ARITHABORT ON
Insert into [SILVX_SN16000]
(
md_group
)
Select
P.value('MD_GROUP[1]','NVARCHAR(255)') AS md_group
From #xml.nodes('/TableData/Row') PropertyFeed(P)
Here is a much-shortened (rows removed) version of my XML:
<?xml version="1.0" ?>
<SilvxInSightImport Version="1.0" Host="uslsss17" Date="14-09-14_20-40-02">
<Tables Count="14">
<Table Name="SN16000">
<TableSchema>
<Column><COLUMN_NAME>PARENT_HPKEY</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
<Column><COLUMN_NAME>MD_GROUP</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
<Column><COLUMN_NAME>PKEY</COLUMN_NAME><DATA_TYPE>NUMBER</DATA_TYPE></Column>
<Column><COLUMN_NAME>S_STATE</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
<Column><COLUMN_NAME>NAME</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
<Column><COLUMN_NAME>ROUTER_ID</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
<Column><COLUMN_NAME>IP_ADDR</COLUMN_NAME><DATA_TYPE>VARCHAR2</DATA_TYPE></Column>
</TableSchema>
<TableData>
<Row><MD_GROUP>100.120.25162</MD_GROUP><PARENT_HPKEY>100</PARENT_HPKEY> <PKEY>161888</PKEY><NAME>UODEDTM010</NAME><ROUTER_ID>10.41.32.129</ROUTER_ID> <IP_ADDR>10.41.32.129</IP_ADDR><S_STATE>IS-NR</S_STATE></Row>
<Row><MD_GROUP>100.120.25162</MD_GROUP><PARENT_HPKEY>100</PARENT_HPKEY> <PKEY>278599</PKEY><NAME>UODEETM010</NAME><ROUTER_ID>10.41.4.129</ROUTER_ID> <IP_ADDR>10.41.4.129</IP_ADDR><S_STATE>IS-NR</S_STATE></Row>
<Row><MD_GROUP>100.120.25162</MD_GROUP><PARENT_HPKEY>100</PARENT_HPKEY> <PKEY>183583</PKEY><NAME>UODEGRM010</NAME><ROUTER_ID>10.41.76.129</ROUTER_ID> <IP_ADDR>10.41.76.129</IP_ADDR><S_STATE>IS-NR</S_STATE></Row>
NT_HPKEY>100</PARENT_HPKEY><PKEY>811003</PKEY><NAME>UODWTIN010</NAME> <ROUTER_ID>10.27.36.130</ROUTER_ID><IP_ADDR>10.27.36.130</IP_ADDR><S_STATE>IS-NR</S_STATE> </Row>
</TableData>
</Table>
</Tables>
</SilvxInSightImport>
The xPath in .nodes() must specify the whole path to the Row nodes so you should start with SilvxInSightImport and work your way down to Row.
/SilvxInSightImport/Tables/Table/TableData/Row
In your case you have multiple table nodes, one for each table and I assume you only need one table at a time. You can use a predicate on the table name in the .nodes() xPath expression.
/SilvxInSightImport/Tables/Table[#Name = "SN16000"]/TableData/Row
Your whole query for SN16000 should look something like this.
select T.X.value('(MD_GROUP/text())[1]', 'varchar(20)') as MD_GROUP,
T.X.value('(PARENT_HPKEY/text())[1]', 'int') as PARENT_HPKEY,
T.X.value('(PKEY/text())[1]', 'int') as PKEY,
T.X.value('(NAME/text())[1]', 'varchar(20)') as NAME,
T.X.value('(ROUTER_ID/text())[1]', 'varchar(20)') as ROUTER_ID,
T.X.value('(IP_ADDR/text())[1]', 'varchar(20)') as IP_ADDR,
T.X.value('(S_STATE/text())[1]', 'varchar(20)') as S_STATE
from #XML.nodes('/SilvxInSightImport/Tables/Table[#Name = "SN16000"]/TableData/Row') as T(X)
You have to sort out the data types used for each column.
SQL Fiddle
Hello I have the following xml structure within a database table column :
DECLARE #Response XML =
'<star:ShowInfo xmlns="http://www.starstandard.org/STAR/5"
xmlns:ns2="http://www.openapplications.org/oagis/9"
xmlns:star="http://www.starstandard.org/STAR/5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="5.1.5"
xsi:noNamespaceSchemaLocation="">
<ShowDataArea>
<ServiceInfo>
<SVPlanInfo>
<AKStatus>
<Code>Error</Code>
<STText xsi:type="ns2:TextType">E12143 - Please fetch me from this xml </STText>
</AKStatus>
</SVPlanInfo>
</ServiceInfo>
</ShowDataArea></star:ShowInfo>'
In the above xml I need to fetch the STText value which is
E12143 - Please fetch me from this xml . Can anyone point me on how I can do it ?
I tried the following but it doesnt seem to work :
;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema' as xsd,
'http://www.w3.org/2001/XMLSchema-instance' as xsi)
SELECT #Response.value('(/xsd:Response)[1]','nvarchar(500)') as ExceptionMessage
What a pain.
remove:
xmlns="http://www.starstandard.org/STAR/5"
It is not a sql issue, but rather namespace-getting-confused issue.
Its heplful totake SQL out the equation sometimes, by testing some place like
http://xpath.online-toolz.com/tools/xpath-editor.php.