How to convert a xml file to sql query - sql

I need some help on converting an xml file to an sql query file. Is it possible to convert it?
<mob_proto isOldStructure="false">
<Mob vnum="101" name="??" localizedname="Cão Selvagem" type="0" rank="0" battle_type="0" level="1" event_type="0" mob_color="0" />
</mob_proto>

Just to give you a sense of how XSLT might be used to do this, consider the following stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="//Mob">
SELECT *
FROM <xsl:value-of select="local-name()" />
WHERE vnum = <xsl:value-of select="#vnum" />
AND name = '<xsl:value-of select="#name" />'
</xsl:template>
</xsl:transform>
This would convert any Mob element to a select query, using 'Mob' as the table name, and filtering on vnum and name as column names. You can play with this here:
http://xsltransform.net/nc4NzQA
The advantages of this approach:
XSLT is a well defined standards based langauge with broad platform support.
XSLT will take care of a lot of XML specific issues very neatly
You could potentially keep all your query transformation logic in one place
Disadvantages:
Your queries will likely be limited to a particular syntax/platform
XSLT can be challenging to learn well and may be difficult for your team to maintain
Another option would be to pass your XML to your database engine. If, for example, you're using SQL Server, you could write a stored procedure that takes an XML parameter and then uses OPENXML (https://msdn.microsoft.com/en-us/library/ms175160.aspx) to extract/parse the XML that is sent in. Postgres also has XML support: http://www.postgresql.org/docs/9.1/static/functions-xml.html, as does Mysql: http://dev.mysql.com/doc/refman/5.5/en/load-xml.html, and Oracle I'm sure does as well.
Advantages to this approach:
May not be as steep a learning curve for a proficient SQL developer who doesn't know XSLT
May be able to easily load data directly into database (either keeping it in XML format or shredding it to tables) more efficiently
Disadvantages:
Support will vary from one DB platform to another
May put additional processing strain on your DB server that could be more efficiently handled by an XSLT processor on another machine (especially if you're dealing with a complex document structure).
Of course, you could also use any number of other custom utilities to do this, but XSLT or SQL XML support are probably your best bets.

Related

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.

Select local xml file into table

I have a column in my table that is set as xslt (XML(.), not null).
Right now I have used a regular string to set it. Like this:
UPDATE table
SET xslt =
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="data">
<table width="400" border="1">
<tr bgcolor="#a0acbc">
</tr>
<xsl:for-each select="row">
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>'
WHERE ....
I would like to do something like this:
UPDATE table
SET xslt = C:\test\Test.xlm
Is this possible?
Could you try
select
cast(x as xml)
from
OPENROWSET (BULK 'C:\test\Test.xml',SINGLE_BLOB) myXml(x)
While it's possible to read things off the drives of the server the database engine is on, it isn't easily possible for SQL Server to read things from the disks of other machines on the network. And in many environments, developers aren't allowed to access the drives of the server running SQL Server.
There are a couple of ways to get what you want, though:
Use an SSIS package to load your stylesheet and update your table.
Write a short program (or ask to have someone write one) that reads in your file and uses the contents as a parameter value in a SQL update statement.
Do you have a way to execute a stylesheet against an arbitrary text file? Then write a stylesheet to wrap the contents of your XSLT file with a SQL update statement, thus generating you a script to use to update your table.

Tool to convert a SQL query into Dynamics CRM FetchXML query

We need a tool that can convert a given well-defined SQL query into it's constituent xml form. Can anyone suggest anything of this sort, a free tool would do great help. Also, can you advise whether FetchXML by MS can be used to achieve this?
We can consider this trivial example to illustrate the requirement...
Input:
select emp_name, emp_id from employee_table where emp_sal > 5000
Output:
<query>
<entity name=”employee_table”>
<attribute name=”emp_name” />
<attribute name=”emp_id”/>
<filter>
<condition attribute=”emp_sal” operator =”>” value=”5000”/>
</filter>
</entity>
</query>
We have recently launched a free online converter which can be used to convert SQL script to CRM FetchXML. The converter is available at http://www.sql2fetchxml.com/. Check it out and let me know if you have any questions about the converter.
Read this: SQL to fetch xml...how to ??

Parse XML string stored on Oracle Table

I've a XML string like that stored on my DB:
<?xml version="1.0" encoding="utf-8"?>
<AddressMaintenance>
<Label Name="lblAddress11">Address Line 1</Label><TextBox Name="txtAddress11">Zuellig Korea</TextBox>
</AddressMaintenance>
do you know if there is a way to extract the value Zuelling Korea using XMLQuery or SQL? I can't create a temporary table because it's a validate environment so I can only read that value. I know is possible using reg exp, but, if possible I try to use XML.
thanks,
Andrea
If this is stored in an XMLTYPE on a table you can use the Oracle XML functions to extract it using XPath:
SELECT
extractvalue(xmlcol, '/*/TextBox[#Name=''txtAddress11'']') txtaddress
FROM yourtable
Adapt the XPath to suit your needs.
See ExtractValue documentation or research other Oracle XML functions.
I should probably note that 11g and later, extractvalue is deprecated and you should use XMLQuery

whats the best way to handle a complex param in a stored proc?

whats the best way to handle a complex param in a stored proc?
let me explain a little about the param so its clear what i am trying to do here...
I have a project management app - so i have things like a release, a project and milestones.
A project has a single release and a release has its milestones.
i am adding reporting and need to generate a report that allows users to select any number of releases and any milestones of a release. this will result in a report that shows all the projects that are part of the release and their data for the milestones selected.
since each release has multiple milestones (milestone table) to keep the associations of what milestone was selected in what release i was thinking to have coma separated list like this to pass the UI data to SQL.
release1 | m2, m3, m4
release2 | m2, m7
release3 | m5
as a varchar or maybe xml...
whats the best practice for sending in something like this that has relational data built into the param? am i way off and over thinking the problem?
please tell me there is a simple solution i am not seeing...
XML would be the best way and simplest with least amount of code... see a sample below.
As starter sample:
DECLARE #Param XML
SET #Param = '
<chriteria>
<release id="1">
<milestone id="1" />
<milestone id="2" />
<milestone id="3" />
</release>
<release id="2">
<milestone id="1" />
<milestone id="2" />
</release>
</chriteria>
'
SELECT c.value('../#id', 'INT') AS ReleaseId, c.value('#id', 'INT') AS MilestoneId
FROM #Param.nodes('/chriteria/release/milestone') AS T(c)
SQL Server 2008 has Table Valued Parameters to solve just this problem. For SQL Server 2005, you will have to either pass in a string to parse in the Stored Proc, or have your caller and Stored Proc use some shared (or temporary) table to hold the data to process.