简体   繁体   中英

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?

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);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM