I have a table called customer, and I have the columns id, sortcode, and name.
I have one more table called tblxml, it has one column called xmlData of type CLOB and it stores the XML data.
I have stored XML data as follows:
<root>
<nd>
<id>1</id>
<sc>001</sc>
</nd>
<nd>
<id>2</id>
<sc>001001</sc>
</nd>
<nd>
<id>11</id>
<sc>001001001</sc>
</nd>
<nd>
<id>16</id>
<sc>001001001001</sc>
</nd>
<nd>
<id>13</id>
<sc>001001002</sc>
</nd>
<nd>
<id>9</id><sc>001002</sc>
</nd>
<nd>
<id>14</id>
<sc>001002001</sc>
</nd>
</root>
I have aligned it so it is easy to understand.
I need to:
extract this XML data from tblxml and update it in customer
table.
extract Id from tblxml table and update the sortcode in customer table which matches to id.
loop the XML data in update the sortcode according to id.
I have used <nd> to separate the rows.
As I'm new to Oracle, I haven't tried coding for this. Expecting some suggestions.
Sounds like XMLTABLE is what you're after:
with sample_data as (select '<root><nd>
<id>1</id>
<sc>001</sc>
</nd>
<nd>
<id>2</id>
<sc>001001</sc>
</nd>
<nd>
<id>11</id>
<sc>001001001</sc>
</nd>
<nd>
<id>16</id>
<sc>001001001001</sc>
</nd>
<nd>
<id>13</id>
<sc>001001002</sc>
</nd>
<nd>
<id>9</id><sc>001002</sc>
</nd>
<nd>
<id>14</id>
<sc>001002001</sc>
</nd></root>' xdata from dual)
-- end of mimicking a table called "sample_data" containing your xml data. See the main SQL below:
select id,
sc
from sample_data sd
cross join xmltable('/root/nd'
passing xmltype(sd.xdata)
columns id number path 'id',
sc varchar2(10) path 'sc');
ID SC
---------- ----------
1 001
2 001001
11 001001001
16 0010010010
13 001001002
9 001002
14 001002001
Once you have the query to pull the results from the xml column, you can then use that as part of an update or merge statement to do the necessary updating. This is left as an exercise for you to do - there are plenty of examples in StackOverflow and Google! If you get stuck, feel free to update your question with what you've tried.
I have solved this problem.
Created new Temporary table called TMP_XML it has one column as hold_xml
Inserting xml content from tblxml to TMP_XML table
Then extracting value from TMP_XML as follows and updating sortcode based on matched Id.
Insert InTo TMP_XML
Select XmlType(PkgCBDmlXml.ExtractNodeValues(xmlData, 'root'))
From tblxml;
update customer
set sortcode = ( Select ExtractValue(Value(E),'//sc') Sc_New
From TMP_XML A,
TABLE(XmlSequence(Extract(A.hold_xml, '//nd'))) E
Where ExtractValue(Value(E),'//id') = id
)
Related
Here is my XML:
<Triggers>
<Trigger>
<Name>DrugName</Name>
<Values>
<Value>Meclofenamate</Value>
<Value>Meloxicam</Value>
<Value>Vimovo</Value>
<Value>Nabumetone</Value>
<Value>Qmiiz</Value>
<Value>Tolmetin</Value>
</Values>
</Trigger>
<Trigger>
<Name>State</Name>
<Values>
<Value>MI</Value>
</Values>
</Trigger>
<Trigger>
<Name>BenefitType</Name>
<Values>
<Value>Pharmacy</Value>
</Values>
</Trigger>
<Trigger>
<Name>LineOfBusiness</Name>
<Values>
<Value>Medicaid</Value>
</Values>
</Trigger>
</Triggers>
My goal is to get output that looks like this:
ID DrugName State BenefitType LineOfBusiness
6500 Meclofenamate MI Pharmacy Medicaid
6501 Meloxicam MI Pharmacy Medicaid
6502 Vimovo MI Pharmacy Medicaid
6503 Nabumetone MI Pharmacy Medicaid
6504 Qmiiz MI Pharmacy Medicaid
6505 Tolmetin MI Pharmacy Medicaid
I can't find any examples on stackoverflow after extensive searches where XML is organized this way, and the examples I have found, tweaked and applied result in my getting a list of all the Values in one column (State values, BenefitType values, etc. mixed in with DrugName values).
The ID column is not part of the XML, but I need to have that in my output.
Here what the table looks like that has the XML column.
You needs the .nodes XML function to break out the Trigger nodes, then again for Values rows.
To get the value of a node instead of it's name, we use text().
To verify we are grabbing the right Trigger node for each column, we use the [] predicate to check (a bit like a where).
.value requires a single value, so we use [1] to get the first node.
SELECT
DrugName = drugs.DrugName.value('text()[1]','nvarchar(100)'),
State = tr.Trigg.value('Trigger[Name/text()="State"][1]/Values[1]/Value[1]/text()[1]', 'nvarchar(100)'),
BenefitType = tr.Trigg.value('Trigger[Name/text()="BenefitType"][1]/Values[1]/Value[1]/text()[1]', 'nvarchar(100)'),
LineOfBusiness = tr.Trigg.value('Trigger[Name/text()="LineOfBusiness"][1]/Values[1]/Value[1]/text()[1]', 'nvarchar(100)')
FROM #xml.nodes('/Triggers') tr(Trigg)
OUTER APPLY tr.Trigg.nodes('Trigger[Name/text()="DrugName"][1]/Values/Value') drugs(DrugName)
I am struggling at outputting XML the correct way. I'm trying to generate XML document with SQL Server query, which gives my the following:
CODE USED:
SELECT Plate, tbl_veiculos.ID, Brand, Model, Origin, Color
FROM tbl_veiculos, tbl_veiculo_spec
WHERE tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_veiculos.ID = 1
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList')
RESULT:
<VehicleList>
<Vehicle>
<Plate>34-23-nd</Plate>
<ID>1</ID>
<Brand>Mercedes-Benz</Brand>
<Model>A140</Model>
<Origin>Germany</Origin>
<Color>Red</Color>
</Vehicle>
</VehicleList>
Which is to a certain way what I need. The problem comes when I try to merge it with other query output. I know this doesn't explain well so I'll show you an harcoded version of what I want.
<VehicleList>
<Vehicle>
<Plate>34-23-nd</Plate>
<ID>1</ID>
<Brand>Mercedes-Benz</Brand>
<Model>A140</Model>
<SellerInfo>
<Name>Someone Special</Name>
<Street>Oxfod Court 1231</Street>
</SellerInfo>
<Origin>Germany</Origin>
<Color>Red</Color>
</Vehicle>
</VehicleList>
<SellerInfo> comes from other table.
You can use subquery as below:
SELECT Plate, tbl_veiculos.ID, Brand, Model,
(select Name, Street from sellerInfo where id = t1.id --use id to join sellerInfo table
for xml Path(''), type) as SellerInfo,
Origin, Color,
FROM tbl_veiculos t1 inner join tbl_veiculo_spec ts
on tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_veiculos.ID = 1
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList')
The scope of this project is much larger than this one question. I've been tasked with a project and I'll not bore you with the intimate details. Ultimately what I'm needing to do is get the data out of the database and into XML so I can convert to JSON and create a simple web app that will allow me to parse and format the data in way that will meet the customer's needs.
I'm sure there's a better way to do this, but this is the path I've settled on..
I have about 46,000 records dumped into a Temp Table. To Give you an idea of how this data is structured running the following query:
SELECT
TransactionID,
OwnerID,
Date,
TransactionType,
ChargeCode,
Description,
DebitAmount
FROM #OwnerHistoryTemp
WHERE OwnerID = '11111111'
Returns this:
TransactionID OwnerID Date TransactionType ChargeCode Description DebitAmount
28727 11111111 2014-12-01 E A1 APPLY CHARGES 210.00
28728 11111111 2014-12-03 C A1 DB11111111 210.00
28729 11111111 2015-01-01 E A1 APPLY CHARGES 183.37
What I'm looking to do here is use the SQL FOR XML PATH (open to any other suggestions) to output the data like so:
<OwnerHistory>
<OwnerID OwnerID="11111111">
<Transactions>
<TransactionID ID="28727">
<Date>2014-12-01</Date>
<TransactionType>E</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>210.00</DebitAmount>
</TransactionID>
<TransactionID ID="28728">
<Date>2014-12-03</Date>
<TransactionType>C</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>DB11111111</Description>
<DebitAmount>210.00</DebitAmount>
</TransactionID>
<TransactionID ID="28729">
<Date>2015-1-01</Date>
<TransactionType>E</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>183.37</DebitAmount>
</TransactionID>
</Transactions>
</OwnerID>
</OwnerHistory>
The Query I have, gets me close, but not QUITE there. Because the same OwnerID appears multiple times (once for each TransactionID), running the following query:
SELECT
OwnerID AS "#OwnerID",
TransactionID AS "Transaction/#RecordID",
Date AS "Transaction/Date",
TransactionType AS "Transaction/TransactionType",
ChargeCode AS "Transaction/ChargeCode",
Description AS "Transaction/Description",
DebitAmount AS "Transaction/DebitAmount"
FROM #OwnerHistoryTemp
WHERE OwnerID = '11111111'
GROUP BY OwnerID, RecordID, Date, ChargeCode, Description, DebitAmount
order by OwnerID
FOR XML PATH ('OwnerID'), ROOT('OwnerHistory')
Returns the folowing:
<OwnerHistory>
<OwnerID OwnerID="11111111">
<Transaction RecordID="28727">
<Date>2014-12-01</Date>
<TransactionType>E</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>210.0000</DebitAmount>
</Transaction>
</OwnerID>
<OwnerID OwnerID="11111111">
<Transaction RecordID="28728">
<Date>2014-12-03</Date>
<TransactionType>C</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>DB11111111</Description>
<DebitAmount>210.0000</DebitAmount>
</Transaction>
</OwnerID>
<OwnerID OwnerID="11111111">
<Transaction RecordID="28729">
<Date>2015-01-01</Date>
<TransactionType>E</TransactionType>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>183.3700</DebitAmount>
</Transaction>
</OwnerID>
</OwnerHistory>
Any thoughts on how to only pull the OwnerID one time as the Parent and group all transactions underneath it?
Might be something simple I'm just not understanding, or it might be impossible. Feel free to publicly cane me if the former is the case...
Ready for your caning?
Nest your queries to get nested XML, something like this:
SELECT TOP 1
OwnerID AS "#ID",
(SELECT
TransactionID AS "Transaction/#ID",
[Date] AS "Transaction/Date",
TransactionType AS "Transaction/Type",
ChargeCode AS "Transaction/ChargeCode",
[Description] AS "Transaction/Description",
DebitAmount AS "Transaction/DebitAmount"
FROM OwnerHistory
WHERE OwnerID = [Owner].OwnerID
FOR XML PATH(''), TYPE) Transactions
FROM OwnerHistory [Owner]
WHERE OwnerID = '11111111'
FOR XML PATH('Owner'), ROOT('OwnerHistory'), TYPE
Resulting XML:
<OwnerHistory>
<Owner ID="11111111">
<Transactions>
<Transaction ID="28727">
<Date>2015-03-26</Date>
<Type>E</Type>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>210.0000</DebitAmount>
</Transaction>
<Transaction ID="28728">
<Date>2015-03-26</Date>
<Type>C</Type>
<ChargeCode>A1</ChargeCode>
<Description>DB11111111</Description>
<DebitAmount>210.0000</DebitAmount>
</Transaction>
<Transaction ID="28729">
<Date>2015-03-26</Date>
<Type>E</Type>
<ChargeCode>A1</ChargeCode>
<Description>APPLY CHARGES</Description>
<DebitAmount>183.3700</DebitAmount>
</Transaction>
</Transactions>
</Owner>
</OwnerHistory>
Note that the TOP 1 is only included to avoid repeating the entire set of transactions for each row containing the OwnerID. This could be handled in a number of ways; normally this sort of nesting would be the result of a join on two normalized tables so that only one instance of Owner would occur.
MSDN has some good examples to demonstrate this technique.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
My table structure is as follows:
date Id Value
--------------------------------
01/01/2005 50000 5
01/01/2006 50000 6
01/01/2007 50000 7
01/01/2005 50001 8
01/01/2006 50001 9
01/01/2007 50001 10
I would like to output the xml using for xml in SQL Server in the following format:
<Date date = "01/01/2005"> <Id dealId=50000" value="5"/> <Id dealId=50001" value="8"/> </Date> <Date date = "01/01/2006"> <Id dealId=50000" value="6"/> <Id dealId=50001" value="9"/> </Date>
It would be of great help if someone can help me out with the query in SQL Server. I have tried couple of them myself but I am not getting the exact output.
-- using your provided values:
DECLARE #table TABLE ([date] DATE, id INT, value INT);
INSERT #Table VALUES ('01/01/2005',50000,5);
INSERT #Table VALUES ('01/01/2006',50000,6);
INSERT #Table VALUES ('01/01/2007',50000,7);
INSERT #Table VALUES ('01/01/2005',50001,8);
INSERT #Table VALUES ('01/01/2006',50001,9);
INSERT #Table VALUES ('01/01/2007',50001,10);
-- XML Query
-- the outer query groups by date, ensuring each date only shows up once
SELECT
[date] AS '#date',
(
-- The inner query selects all Ids for each date found in the outer query
SELECT id as '#dealId',
value as '#value'
FROM #Table B
WHERE B.date=A.date -- join on date from outer query
FOR XML PATH ('Id'), TYPE
)
FROM #Table A
GROUP BY date
FOR XML PATH ('Date');
-- Produces:
<Date date="2005-01-01">
<Id dealId="50000" value="5"/>
<Id dealId="50001" value="8"/>
</Date>
<Date date="2006-01-01">
<Id dealId="50000" value="6"/>
<Id dealId="50001" value="9"/>
</Date>
<Date date="2007-01-01">
<Id dealId="50000" value="7"/>
<Id dealId="50001" value="10"/>
</Date>
Usually, if I need such a simple xml transformation, I'm using for xml raw:
select
t1.[date],
(
select
t2.[Id] as dealId, t2.[Value]
from Table1 as t2
where t2.[date] = t1.[date]
for xml raw('Id'), type
)
from Table1 as t1
group by t1.[date]
for xml raw('Date')
----------------------
<Date date="2005-01-01">
<Id dealId="50000" Value="5"/>
<Id dealId="50001" Value="8"/>
</Date>
<Date date="2006-01-01">
<Id dealId="50000" Value="6"/>
<Id dealId="50001" Value="9"/>
</Date>
<Date date="2007-01-01">
<Id dealId="50000" Value="7"/>
<Id dealId="50001" Value="10"/>
</Date>
Try it yourself in the sql fiddle demo
Some notes:
You don't have to use # in column names, because for xml raw is attribute-centric by default.
Subquery here is necessary, because you want nested xml and for now it's the only way to get it.
You also have to use type modifier in the subquery to get inner data as xml.
So I have the below that will allow me to get xml from one record in the Products table.
However, I need to to be able to get the XML from ALL records in the Products table, along with its ProductId.
I'm confused on how to start this.
DECLARE #MyXML XML
SET #MyXML =
(SELECT ProductsXML
FROM Products
WHERE ProductId= 1)
SELECT
a.b.value('#upccode','int') as UPC,
a.b.value('#dateadded','date') as DateAdded
FROM
#MyXML.nodes('xml/Product/UPC')a(b);
I'm using SQL Server 2008.
Test Data:
ProductId: 1
ProductsXML:
<xml>
<Product>
<UPC upccode="1237" dateadded="10/9/2012"/>
<UPC upccode="1236" dateadded="10/8/2012"/>
<UPC upccode="1235" dateadded="10/7/2012"/>
<UPC upccode="1234" dateadded="10/6/2012"/>
</Product>
</xml>
ProductId: 2
ProductsXML:
<xml>
<Product>
<UPC upccode="9876" dateadded="9/9/2012"/>
<UPC upccode="9877" dateadded="9/8/2012"/>
<UPC upccode="0998" dateadded="9/7/2012"/>
<UPC upccode="7877" dateadded="9/6/2012"/>
</Product>
</xml>
The Result I'm looking for is something like this:
ProductId UPC DateAdded
--------- --- ---------
1 1237 10/9/2012
1 1236 10/8/2012
....
2 9876 9/9/2012
2 9877 9/8/2012
Right now I can get the above but ONLY by specifying one PoductId at a time. I want to be able to run all of the `Products' without specifying each entry.
I guess you are looking for something like this.
SELECT ProductId,
a.b.value('#upccode','int') as UPC,
a.b.value('#dateadded','date') as DateAdded
FROM Products
CROSS APPLY ProductsXML.nodes('xml/Product/UPC')a(b);
There is no need to have a XML variable in there. Query the Products table directly and use CROSS APPLY against ProductsXML field to shred your XML.
hi u can try using Linq to xml to create an entity from the xml, see the sample code below
XDocument somedoc = XDocument.Load("your xml path");
var somelist = from somevariable in somedoc.Descendants("Product")
select new Product {
UPC = somevariable.Attribute("upccode").value
DateAdded = somevariable.Attribute("dateadded").value
}
so now somelist is a collection thru which u can iterate and add to List<Product>, create product b4 u start tryinh this