Conditional insert xquery failing with error "unexpected insert, expecting else" - xquery

I am trying to execute the xquery on Sedna database to conditionally update the container, as below
if(fn:exists(doc("blog")/entries/entry[#active="1" and #id="1"]/comments)) then
UPDATE insert <comment active="1"><name>sunil.s</name><email>suni.l.s#gmail.com</email><desc>desbbh</desc></comment> into doc("blog")/entries/entry[#active="1" and #id="1"]/comments
else
UPDATE insert <comments><comment active="1"><name>sunil.s</name><email>sunil.s#gmail.com</email><desc>sdd</desc></comment></comments> into doc("blog")/entries/entry[#active="1" and #id="1"]
But this query always failing with below error
SEDNA Message: ERROR XPST0003 It is a static error if an expression is
not a valid instance of the grammar defined in A.1 EBNF. Details: at
(2:8), syntax error, unexpected insert, expecting else
The error indicate that it is expecting else instead of insert in the second line.
Can someone please help me understand problem with the query and possible fix?

Your query assumes the existence of an expression with syntax like
UPDATE insert expr into expr
There is no such expression in XQuery (or the XQuery Update Facility). Instead, it appears to be a non-standard syntax supported by Sedna.
However, the documentation (http://www.sedna.org/progguide/ProgGuidesu6.html#x12-430002.3) refers to it as a "statement", not an "expression", suggesting that it must be the 'top-level' (outermost) construct in your query.
To accomplish this, you could rewrite your query as:
UPDATE
insert
if ... then <comment>...</comment> else <comments>...</comments>
into
if ... then doc("blog")/.../comments else doc("blog")/...
Unfortunately, this repeats the 'if' condition; it's not clear whether Sedna provides a syntax for factoring that out, e.g.
UPDATE
let $c := ...
insert
if $c then <comment>...</comment> else <comments>...</comments>
into
if $c then doc("blog")/.../comments else doc("blog")/...

Related

CREATE FUNCTION throws SQL Error (1064) (42000)

I'm trying to create a stored function in a MariaDB database.
I copied the function I'm trying to create from the MariaDB Docs:
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;
Unfortunately, I get the following error:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
What baffles me most is that the given code is supposed to resolve the very error code I'm getting according to the MariaDB docs
The solution is to specify a distinct delimiter for the duration of the process, using the DELIMITER command
It turned out, the client I used to issue the command, DBeaver, was causing the trouble.
After switching over to MySqlWorkbench everything worked as expected.
Apparently, DBeaver didn't recognise the delimiters correctly..
I think you may have forgotten to select the database you want this routine to be stored into.
So try adding a use as the first line
use `test`;
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;

neo4j apoc - extraneous input '('

what am I doing wrong with the query
WITH [1] AS a, [2] AS b
RETURN apoc.coll.union(a,b);
Although it returns the result ([1,2]) In the browser it keeps telling me extraneous input '(', expecting...
Is this a problem or just "Lint garbage"? I am trying to identify a problem with another query where the same Lint message pops up in the same kind of use of the apoc function.
EDIT:
As discussed in the comments, CALL is not appropriate to functions (like apoc.coll.union). So I believe this behavior is a bug in the lint of Neo4j Browser. I opened an issue in the Neo4j Browser repo.
ORIGINAL ANSWER:
I believe the problem is that a user defined procedure (like apoc.coll.union) should be called with CALL and not after the RETURN statement. You can try something like:
WITH [1] AS a, [2] AS b
CALL apoc.coll.union(a,b) as r
RETURN r

print out xquery sequence and exit

Is there a way to "die" in execution flow in an xquery file and output a nicely formatted printout of a sequence variable?
I'm trying something like:
return { fn:error(xs:QName("ERROR"), $xml) }
but that doesn't quite seem to work.
Thanks!
Based on your comment (you need it for debugging) I guess you are looking for the fn:trace function, described here http://www.xqueryfunctions.com/xq/fn_trace.html
If you want to abort the execution flow and output an error in your application you should in fact use the XQuery exception handling.
Try something like this, omitting the return if this isn't part of a FLWOR expression.
...
return fn:error((), "DEBUG", $xml)
There's no need for curly braces unless you're enclosing an expression, for example <x>{ current-time() }</x>. The return expression is not enclosed.
With MarkLogic it's best to leave the first parameter of fn:error empty. That way you don't have to worry about a QName, and anyway some folks believe that it's reserved for predefined errors. MarkLogic uses the second parameter to fill in error:code, and the third parameter for data.
For more on fn:error, see http://docs.marklogic.com/fn:error and https://github.com/robwhitby/xray/pull/11

PL/SQL script giving errors

I've to work on some older EDW scripts, which I think are in PL/SQL and queries fech data from Oracle table as well. But there is some problem with them, the part which declares variables, as shown in the image gives error. I'm unable to understand why?
Below is some part of script,
VARIABLE begin_exp_date varchar2(8)
VARIABLE end_exp_date varchar2(8)
VARIABLE begin_cal_key number
Declare
begin
:begin_exp_date := 'begin_exp_date';
:end_exp_date := 'end_exp_date';
:begin_cal_key := 'begin_cal_key';
end;
These lines produce error ORA-00900: Invalid SQL statement.
Any help?
If you plug the script in SQL*Plus, it will be executed without the ORA-00900 error. I guess you received the error when it was run in Toad.
If it is indeed PL/SQL, it should be more like this
DECLARE
begin_exp_date varchar2(8);
end_exp_date varchar2(8);
begin_cal_key number;
BEGIN
begin_exp_date := 'begin_exp_date';
-- and so on
END;
You can set constant values to the variables in the DECLARE section if you want. Note that you've defined begin_cal_key as a NUMBER so cannot assign the string 'begin_cal_key'

Marklogic Xquery fn:data(<type>hello world</type>) giving Invalid lexical value error

My Marklogic XQuery fn:data(<type>hello world</type>) gives me Invalid lexical value error
This is the stack trace:
query evaluated in Documents at file::Docs/ as 1.0-ml (cq v4.1-1-EA)
[1.0-ml] XDMP-LEXVAL: xs:integer("hello world") -- Invalid lexical value "hello world"
Stack trace:
line 2:
1:
2: fn:data(<type>hello world</type>)
xdmp:eval("
fn:data(<type>hello world</type>)", (), <options xmlns="xdmp:eval"><isolation>different-transaction</isolation></options>)
in /cq/eval.xqy line 111:
And when I use fn:data(<p>hello world</p>) it is giving me expected answer (hello world).
Any help on this error would be appreciated.
Thanks.
Finally figured what was wrong.
Had an xsd which was defining element type as xs:integer thats why this error was coming. Removed that xsd and now everything is working fine :)
Glad you figured it out. It looks like the extra schema was targeting the empty namespace?
Here's a tip: avoid using a schema unless it also specifies a namespace. That makes it much easier to keep your schemas organized.

Resources