hys564219
hys564219

Reputation: 33

How to make orientDB avoid null

When I execute code below,

select expand(distinct(@rid)) from (
  select from V
  where @rid = 'number not exist'
)

it returns

OCommandExecutionException:
     expression item '@rid' cannot be resolved because current record is NULL

This inside select is just for test and it returns no record.

I want null to be returned if select returns no record and expand(distinct(@rid)) if select returns some @rid.

The environment is OrientDB 2.1.5 and it occurs both in console and studio.

Thanks!

Upvotes: 2

Views: 349

Answers (3)

Sebastian
Sebastian

Reputation: 1263

You can do:

SELECT expand(ifnull(@rid, 'null')) 
FROM V WHERE @rid = 'number not exist'

It will expand the record if exists, otherwise return empty.

Upvotes: 0

peak
peak

Reputation: 116690

Your query works correctly in version 2.1.7:

OrientDB console v.2.1.7-SNAPSHOT (build 4) www.orientdb.com
...
orientdb> connect ...

orientdb {db=demo}> select expand(distinct(@rid)) from (select from V where @rid = 'number not exist')

0 item(s) found. Query executed in 0.076 sec(s).

Upvotes: 1

Alessandro Rota
Alessandro Rota

Reputation: 3570

You can create a simple javascript function as the following

var g=orient.getGraph();
var b=g.command("sql","select from V where @rid = #12:0");
if(b.length>0){
    var c=g.command("sql","select expand(distinct(@rid)) from ( select from V where @rid = #12:0)");
    return c;
}
else
  return null;

and call it from console or studio

select expand(result) from (select yourFunction() as result)

Upvotes: 0

Related Questions