Equivalent SP in MySQL for sp_xml_preparedocument of SQL Server - mysql

I have two queries -
What would be the equivalent sp\function name in MySQL for sp_xml_preparedocument of SQL Server?
What would be the equivalent sp\function name in MySQL for OPENXML of SQL Server?

In sp_xml_preparedocument \ openXML, I can directly put my XML and can use as a table. But, in case of ExtractValue and UpdateXML, I have to insert each xml row in seperate row of the table and then select. Concedering the following example -
if I want to use '<books>
<book>
<title>A guide to the SQL standard</title>
<author>
<initial>CJ</initial>
<surname>Date</surname>
</author>
</book>
<book>
<title>SQL:1999</title>
<author>
<initial>J</initial>
<surname>Melton</surname>
</author>
</book>
</books>'
This xml I can use directly in SQL Sql Server. But in case of MySQL, I have to insert two row into the table then use ExtractValue function.

To start with, SQL Server itself has moved off sp_xml_preparedocument and openXML - these are procedural functions that use handles - very unwieldy.
In MySQL, you can use the XML functions ExtractValue and UpdateXML http://dev.mysql.com/doc/refman/5.5/en/xml-functions.html
And here's a very short intro on how to use them http://dev.mysql.com/tech-resources/articles/mysql-5.1-xml.html

Related

Select value in xml field with mysql

I have a field in a sulu cms database.
Table phpcr_nodes and field props
This field is stocked in XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<sv:node xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:mix="http://www.jcp.org/jcr/mix/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:rep="internal" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<sv:property sv:name="i18n:en-description" sv:type="String" sv:multi-valued="0">
<sv:value length="2560">MY FIRST ITEM</sv:value>
</sv:property>
<sv:property sv:name="i18n:en-subtitle" sv:type="String" sv:multi-valued="0">
<sv:value length="28">MY SECOND ITEM</sv:value>
</sv:property>
</sv:node>
I would like to catch the value
<sv:value length="2560">MY FIRST ITEM</sv:value>
and
<sv:value length="28">MY SECOND ITEM</sv:value>
I have tried something like this:
SELECT
SUBSTRING_INDEX(ExtractValue(props, '//sv:name="i18n:en-subtitle"'), ' ', 1) AS `subtitle`
from phpcr_nodes
It's a MySQL database, how can I get the value in SQL ?
Thanks in advance
If you need to write queries to PHPCR you should use the jackalope abstraction and its query builder so you can write normal SQL 2 queries and jackalope will convert them based on your adapter into the correct SQL query.
If you are interested how jackalope is converting SQL 2 query into SQL queries have a look at its QOMWalker implementation:
https://github.com/jackalope/jackalope-doctrine-dbal/blob/master/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
EXTRACTVALUE(props, '//sv:property[#sv:name="i18n:en-subtitle"]/sv:value')
The "EXTRACTVALUE" is a XPATH so you can also copy your XML into a website like http://xpather.com/ to find the correct xpath for your property selection.

How to use the CSV query language?

In JasperSoft Studio or iReport, how do you query on csv data? It has an option to do CSV query language but I cannot find any proper documentation or instructions on how it works.
I need to do group by and add conditional parameters without the need to transfer it first into a database.
The JRCsvQueryExecuter lets you sort and filter data from a cvs file
The sort command can be done on single or multiple fields.
<sortField name="name"/>
<sortField name="city" order="Descending"/>
The filtering is done by the filterExpression
<filterExpression><![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]></filterExpression>
You can not use a normal sql statement but with these 2 properties, you are fairly close to order by and where. Specially since jasper reports have build in support for sum, avg (through variables) and the group by through groups.
<group name="YourGroup">
<groupExpression><![CDATA[$F{fieldToGroupOn}]]></groupExpression>
...the group bands ..
</group>
You can find a full running sample in the jasper reports distribution under demo\samples\csvdatasource\reports\CsvQueryExecuterReport.jrxml, this is the sample reference

SQL Server equivalent of MySQL Load xml infile

I have been using the MySQL Load XML Infile to load an xml file into a MySQL tables. The xml file has this format
<Detail_Collection>
<Detail
JobId=“12345”
JobDescription=“Job1”
Sold_To=“Customer1”
/>
<Detail
JobId=“23445”
JobDescription=“Job2”
Sold_To=“Customer2”
/>
</Detail_Collection>
My table looks like this
JobId
JobDescription
Sold_to
Every thing works fine.
I am finding myself having to move from MySQL to SQL Server and can't seem to find a simple way to do this in SQL Server. Am I missing something
If you are looking for a SQL query to fetch the job data from your XML file into SQL Server database table, please check the below T-SQL syntax for querying XML data on SQL Server
declare #xml as xml
set #xml = '
<Detail_Collection>
<Detail
JobId="12345"
JobDescription="Job1"
Sold_To="Customer1"
/>
<Detail
JobId="23445"
JobDescription="Job2"
Sold_To="Customer2"
/>
</Detail_Collection>'
select #xml
select
job.value('#JobId','int') JobId,
job.value('#JobDescription','varchar(400)') JobDescription,
job.value('#Sold_To','varchar(400)') Sold_To
from #xml.nodes('/Detail_Collection/Detail') as jobs(job)
You can find tutorials on querying XML on SQL Server at given reference

List of aggregate functions

Is there a way to fetch list of aggregate functions supported by a dbms using jdbc metadata or running any dbms specific query?
On SQL Server you can query XML which is in installation directory:
DECLARE #xml XML
SELECT #xml = x.y
FROM OPENROWSET( BULK 'C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlToolsData\1033\SQLCommonObjects.xml', SINGLE_BLOB ) x(y)
;WITH XMLNAMESPACES( 'http://tempuri.org/SqlCommonObjects.xsd' AS ns )
SELECT
Category.Name.value('ns:DisplayName[1]', 'VARCHAR(MAX)') [Category],
[Function].Name.value('ns:Name[1]', 'VARCHAR(MAX)') [Function],
[Function].Name.query('for $p in ns:Parameters/ns:Parameter return
concat($p/ns:Name[1],",")').value('.', 'VARCHAR(MAX)') Parameters
FROM #xml.nodes('//ns:Category[ns:DisplayName="Aggregate Functions"]')
AS Category(Name)
CROSS APPLY Category.Name.nodes('ns:Objects/ns:Function') [Function](Name)
Where after BULK statement you should give your folder(difference mainly is Program Files" and "Program_Files(x86)" and SQL server version (100 is 2008 in example)
Your post has multiple DB tags, and each has system catalogs and/or an information schema that would let you know the list of procedures. Which table/view to query will differ from a DB engine to the next, however... (For instance, in Postgres you'd join pg_proc and pg_aggregate, since information_schema.routines won't tell you which procs are aggregates.)
It is usually safe to assume that typical aggregate functions (sum(), count(), avg()...) exist in all database implementations.
The only exception I'm aware of is Postgres, which does not support any()/some() due to ambiguity in the syntax:
SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
There is nothing fullproof, since it is not part of the JDBC spec. If you don't know what database engine you are using at runtime, your best bet is to submit a test query to the database and check whether or not it failed before using any aggregate function that may not be supported.

XPath to check if all provided pairs of elements exist for an entry

I have the following XML stored in an XML field in a SQL Server 2008 database.
<attributes>
<attribute>
<key>animal</key>
<value>rat</value>
</attribute>
<attribute>
<key>color</key>
<value>black</value>
</attribute>
</attributes>
I'm able to select all the entries from the table which have an 'animal' key with a value of 'rats' with the following xpath in sql.
SELECT *
FROM XmlEvents
WHERE Attributes.exist('/attributes/attribute[key="animal" and value="rat"]') = 1
How would I check for matches with multiple attributes?
I'm trying to find rows where: (Key=Animal & Value=Rat) AND (Key=Color & Value=Black).
(Yes, I could put another .exist with an AND in my WHERE clause but I'm trying to avoid that)
Bonus points if you can lead me in the right direction to make this some-what dynamic. I would love to have a stored procedure or function which could somehow take a list of Key/Value pairs and find all the rows which match everything provided.
XPath allows nested predicates and multiple predicates. You could write
/attributes[attribute[key="animal" and value="rat"]][attribute[key="color" and value="black"]]
or equivalently, but somewhat shorter
/attributes[attribute[key="animal"]/value="rat" and attribute[key="color"]/value="black"]