0

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?

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Hi @RomanTkachuk I tried this it didn't work. I received an error.
Are you declare v_nk AS text[]?
Hey @RomanTkachuk ......When I used v_nk : ARRAY ['S001234'::text,'S9900']; I got this error - Array value must start with "{" or dimension information. Then I used 2nd solution It transforms into run time as where nk = ANY("{S001234,S23401,S34509,S9900}") And gives error for "{" bracket
Can you try: SELECT ARRAY ['S001234'::text,'S9900'];
And = ANY('{S001234,S23401,S34509,S9900}'::text[]) - Please use ' NOT "
|
1

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;

2 Comments

Hey @sia I tried this as well Its giving me error.. Doesn't work
You messed up the quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.