Madhu
Madhu

Reputation: 429

How can I use multiple values in SQL variable in PostgresSQL/plpgsql?

I want to declare variable as v_nk varchar;

then assign the variable v_nk := ''S001234','S23401','S34509','S9900'';

and use this variable in select query

select * from mytable where nk in (v_nk);

I want to use this in one of my program(plpgsql) can you please tell me how I should use it?

Upvotes: 0

Views: 97

Answers (2)

Roman Tkachuk
Roman Tkachuk

Reputation: 3266

You can use array :

v_nk := ARRAY [ 'S001234'::text ,'S23401','S34509','S9900'];

OR

v_nk := '{S001234,S23401,S34509,S9900}'::text[];

and use this variable in select query:

select * from mytable where nk = ANY (v_nk);

Upvotes: 3

sia
sia

Reputation: 577

you can define array instead of variable like this:

DECLARE nk_array    VARCHAR(30)[] = '{'S001234','S23401','S34509','S9900' }';
select * from mytable where nk in nk_array;

Upvotes: 1

Related Questions