Question

I have to execute following query:

create dm_myobject object
set my_id_attribute = (select r_object_id from dm_otherobject where <some clause here>)
where ...

But subquery in brackets returns more than one id. I can't make whereclause more detailed to retrieve only one value. How to take first?

ENABLE(FETCH_ALL_RESULTS 1) or ENABLE(RETURN_TOP 1) doesn't help.

Was it helpful?

Solution

In my experience it is impossible to use DQL hints in a sub query like you suggested, because the hint is applied to the query as a whole. It is indeed possible to use, say, ENABLE(RETURN_TOP 1) on a query that contains a sub query, however that hint will then be used on the outer query and never on the inner one. In your case, however, you'll end up with an error message telling that the sub query returns more than one result.

Try using an aggregate function on the selected attribute instead:

CREATE dm_myobject OBJECT
SET my_id_attribute = (
    SELECT MIN(r_object_id)
    FROM dm_otherobject
    WHERE <some clause>
)

The MIN and MAX functions work with ints and strings, and I suspect they work with IDs too. Since it is ok for you to set only the first ID that's returned from your sub query, I suspect you're returning them in a sorted order and want to use the first -- hence the usage of the MIN function.

An alternative approach would of course be to write a script or a small Java program that executes several DQL statements, but that might or might not work for you in your case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top