Re: set value var via execute

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: Peter Kroon <plakroon(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: set value var via execute
Date: 2012-11-29 19:23:23
Message-ID: A76B25F2823E954C9E45E32FA49D70EC08F06944@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

From: Peter Kroon [mailto:plakroon(at)gmail(dot)com]
Sent: Thursday, November 29, 2012 11:01 AM
To: pgsql-general(at)postgresql(dot)org
Subject: set value var via execute

Is it possible to set the value of a var via execute?

drop table if exists __test;
create unlogged table __test(
id int
);

DO $$

DECLARE
v_holder int;
v_table text = 'table';
v_record_0 text[];
v_id int;

BEGIN

execute '
insert into __test(id)
select id from '||v_table||' order by random() limit 2
';
v_id = (select id from __test limit 1);

 --begin this fails------------------------------------------------------------------------------------------
        v_holder = execute 'select id from '||v_table||' order by random() limit 1';
        --end this fails-------------------------------------------------------------------------------------------

v_record_0 := array(
SELECT id FROM table order by random() --limit 2
);

raise notice '%', v_record_0;
END;

$$ LANGUAGE plpgsql;

Peter,

Instead of:

v_holder = execute 'select id from '||v_table||' order by random() limit 1';

do this:

execute 'select id from '||v_table||' order by random() limit 1' INTO v_holder;

Regards,
Igor Neyman

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Serge Fonville 2012-11-29 19:26:51 Re: When does CLUSTER occur?
Previous Message Gavin Flower 2012-11-29 19:05:25 Re: youtube video on pgsql integrity