xdmp:sql( $sql as xs:string, [$options as xs:string*] ) as item()*
Executes an ad hoc SQL query. This function is for testing your SQL views when data modeling; it is not intended to be used directly in applications.
http://marklogic.com/xdmp/privileges/xdmp-sql
Only one of the "map" and "array" options may be specified. If neither is specified, the default is "array". If the "format" option is specified, the output will be formatted, regardless of whether "array" or "map" was selected.
The first tuple returned will always be one consisting of the column names.
xdmp:sql("select title,author from books limit 4", "format") ==> | title| author| | The C++ Programming Language| Bjarne Stroustrup| | Modern Information Retrieval| Ricardo Baeza-Yates| | Modern Information Retrieval| Berthier Ribeiro-Neto| | Unicode Demystified| Richard Gillam|
xdmp:to-json(xdmp:sql("select title,author from books limit 4", "array")) ==> [["title", "author"], ["The C++ Programming Language", "Bjarne Stroustrup"], ["Modern Information Retrieval", "Ricardo Baeza-Yates"], ["Modern Information Retrieval", "Berthier Ribeiro-Neto"], ["Unicode Demystified", "Richard Gillam"] ]
xdmp:to-json(xdmp:sql("select title,author from books limit 4", "map")) ==> [ {"title": "The C++ Programming Language", "author": "Bjarne Stroustrup"}, {"title": "Modern Information Retrieval", "author": "Ricardo Baeza-Yates"}, {"title": "Modern Information Retrieval", "author": "Berthier Ribeiro-Neto"}, {"title": "Unicode Demystified", "author": "Richard Gillam"} ]
for $row in xdmp:sql("select title,author from books limit 4") return fn:concat("Title=",$row[1],"; Author=",$row[2]) ==> Title=title, Author=author Title=The C++ Programming Language; Author=Bjarne Stroustrup Title=Modern Information Retrieval; Author=Ricardo Baeza-Yates Title=Modern Information Retrieval; Author=Berthier Ribeiro-Neto Title=Unicode Demystified; Author=Richard Gillam
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.