Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

PostgreSQL stored procedure to return rows or empty set

$
0
0

I have stored procedure, which RETURNS SETOF ct_custom_type and inside I do

RETURN QUERY EXECUTE 'some dynamic query'

And I want to do this: If this ‘dynamic query’ returns >= 10 rows, I want to return them, but if it returns only < 10 rows, I don’t want to return anything (empty set of ct_custom_type)

I tried

RETURN QUERY EXECUTE 'some dynamic query'

GET DIAGNOSTICS variable = ROW_COUNT;
IF variable < 10 THEN

    # I don't know what to do here or how to accomplish this

END IF;

If I do RETURN QUERY SELECT 0, 0, ''::text; in IF block (because ct_custom_type is composite type of integer, integer, text), it just adds this ‘empty row’ to previous query result, but I don’t want to return anything in this case, I can do RETURN;, but it will return that previous result and I want to discard it.

EDIT //

I have it like this

EXECUTE 'dynamic query';

GET DIAGNOSTICS variable = ROW_COUNT;
IF variable >= 10 THEN
    RETURN QUERY EXECUTE 'dynamic query';
END IF;

and it works, but I didn’t want to do this query two times


Viewing all articles
Browse latest Browse all 1138

Trending Articles