6

In PostgreSql procedure, I need to create a list of integers to use in a delete statement, as in the example:

DELETE FROM appointment_virtual WHERE appointment_id IN (list_delete);

I need help with:

How do I declare a list type variable?
I found the following form, but I was unsure if it serves the purpose of the DELETE statement:

list_delete integer ARRAY;

How do I add items to this list variable?
I found the following way:

list_delete = array_append (_delete, _appointment_id);

How to zero the contents of this list variable?
Is the syntax below correct?

list_delete = [];

Thanks any help!

5
  • Did you have a read of postgresql.org/docs/current/arrays.html? Commented Oct 9, 2019 at 20:36
  • No, [] is not correct. Use either ARRAY[] or '{}'. Commented Oct 9, 2019 at 20:38
  • Also see this topic for the syntax in the WHERE clause: it's either appointment_id = ANY(list_delete) or appointment_id IN unnest(list_delete). Commented Oct 9, 2019 at 20:42
  • IN cannot be used with arrays. Use appointment_id = ANY(list_delete) instead. Commented Oct 9, 2019 at 20:42
  • @Islingre Thank you! It'is correct. Commented Oct 9, 2019 at 20:50

1 Answer 1

15

To define an array variable append [] to the end of the data type:

list_delete integer[];

To assign values use

list_delete := array[1,2];

to append an integer to the array:

list_delete := list_delete||4;

To assign an empty array use:

list_delete := CAST(array[] AS integer[]);

Or set it to null

list_delete := null;
Sign up to request clarification or add additional context in comments.

Comments

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.