Create XML with parent and child element - sql

I have to create an XML file with the below format.
office_tel and mobile are fields of a table.
Any help would be valuable.
<fields>
<office_tel>
<value>1234</value>
</office_tel>
<mobile>
<value>99999</value>
</mobile>
</fields>

Try this for SQL SERVER. Ref FOR XML (SQL Server)
SELECT
office_tel as "office_tel/value",
mobile as "mobile/value"
FROM TableName
FOR XML PATH('fields')

Related

Oracle 12c: Extract data (select) from XML (CLOB Type)

I need to extract some tag values from a XML code saved in a CLOB type column in Oracle 12c table.
Earlier we were using xmltype(COLUMN).extract('XPath/text()').getStringVal() to extract data from tags but its not working after our database upgrade to 12c.
We have XML Like:
<otm:ShipmentStatus
xmlns:gtm="http://xmlns.oracle.com/apps/gtm/transmission/v6.4"
xmlns:otm="http://xmlns.oracle.com/apps/otm/transmission/v6.4">
<otm:ServiceProviderAlias>
<otm:ServiceProviderAliasQualifierGid>
<otm:Gid>
<otm:Xid>GLOG</otm:Xid>
</otm:Gid>
</otm:ServiceProviderAliasQualifierGid>
<otm:ServiceProviderAliasValue>TEST.123</otm:ServiceProviderAliasValue>
</otm:ServiceProviderAlias>
<otm:IntSavedQuery>
<otm:IntSavedQueryGid>
<otm:Gid>
<otm:DomainName>TEST</otm:DomainName>
<otm:Xid>FIND_DELIVERY_NUMBER</otm:Xid>
</otm:Gid>......etc.
From this XML we have to select some values.
Please suggest some way to solve this problem. Feel free to ask if you need anything more.
Thank You.
Satyam
Example xml has namespaces. And you have to use it.
xmltype.extract('/otm:ShipmentStatus', 'xmlns:gtm="http://xmlns.oracle.com/apps/gtm/transmission/v6.4"
xmlns:otm="http://xmlns.oracle.com/apps/otm/transmission/v6.4"') extacting node from specify namespace
xmltype.extract('/*:ShipmentStatus') extractin node from any namespace

how to insert the xml data to sql server using c#?

I am beginner to the C#, I need to insert my XML file to SQL table pro-grammatically XML file have five nodes called root, name, subject, sno and queries, I will listed below
<root>
<name>
MyName
</name>
<subject>
newsubject
</subject>
<queries>
<sno>
1
</sno>
<query>
This is the query one
</query>
</queries>
<queries>
<sno>
2
</sno>
<query>
This is the query two
</query>
</queries>
<queries>
<sno>
3
</sno>
<query>
This is the query three
</query>
</queries>
</root>
I need to add this value to sql server table name member_info
my table design is
name --> varchar(50)
subject-->varchar(75)
sno-->int
queries-->varchar(150)
I tried some basic stuffs for finding the XML file on the specific folder, I don`t have idea how to implement by reading and inserting XML file, Please be gentle I am just beginner.
You have a few options:
You can read the XML nodes before inserting into the database, and
insert normalized table column values.
You can shred the XML in a stored procedure, by passing the XML
string as an input parameter. You can read up on how to shred the XML utilizing the nodes() method, here.

Replace xml in SQL

I'm really new to SQL and I'm trying to replace a certain XML record with a brand new updated XML.
So i have a table that contains a list with records that all have a barcode ID, lets say barcode 123 has a column named XML that is filled with an XML A. I want to replace XML A with XML B that is saved on my hard drive.
Can anyone help me with this? It seems i can only find ways to directly replace data inside the xml itself, not replacing the entire XML.
I am using SQL server management studio 2012.
Thanks in advance.
Why not use an update query
update table_name set xml='all content of your xml file here' where BarcodeID=123;

What is the simplest way to convert table Rows into XML and viceversa

I need to develop a sql server procedure which will convert table rows into xml and will transfer it to another stored procedure(Different server) through link server. On that server I have to take this xml data as input and again convert it into table rows. What will be the simplest and time efficient way to do this.
It is not possible to use the XML datatype as a parameter in stored procedure on a linked server. You have to use nvarchar(max) and convert to XML in the stored procedure.
To create the XML you should use FOR XML (SQL Server). RAW and AUTO is easy and if you need more control you could use PATH. I would stay away from EXPLICIT.
To shred the XML on the receiving side you should use nodes() Method (xml Data Type) and value() Method (xml Data Type).
Given that you are using SQL Server, you need only append FOR XML to your query.

xpath/xquery sql - Get all values

Hi I have a blob of xml..
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
<string>11</string>
<string>1211</string>
<string>12331</string>
how would I get all the values using xpath/xquery in SQL
Thanks
The xpath //string will return all the values in the intire xml the xpath /string will return only the values in the root node.
And for using it in sql look at this post XPath to fetch SQL XML value
in oracle database
you have XMLType (see this) data type..
by this datatype you can get all the value..
all the examples are there itself, please go through that site.