Ah so. Good catch. Yes, the position of the RETURN clause does appear to matter. So making this change does eliminate the exception, but unfortunately, it doesn't produce the desired result. In this case, no record is returned
except when the UPDATE succeeds. What I need is essentially the opposite: If the update doesn't succeed (because the target key already exists), I want to get the record back (if I also get back the newly inserted item when the update does succeed, that's fine, but not really necessary). With MSSQL, I can do this easily in a single atomic operation, but so far, I've not been able to come up with a solution for OrientDB. I suppose a function or batch that executes the update followed by a select would work, but I was hoping to find something more efficient.