Обсуждение: How to set array element to null value

Поиск
Список
Период
Сортировка

How to set array element to null value

От
Brahmam Eswar
Дата:
I'm trying to reset array element to null. but 3rd line of below snippet is giving the compilation error.


FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;

--
Thanks & Regards,
Brahmeswara Rao J.

Re: How to set array element to null value

От
Pavel Stehule
Дата:


2018-07-09 11:58 GMT+02:00 Brahmam Eswar <brahmam1234@gmail.com>:
I'm trying to reset array element to null. but 3rd line of below snippet is giving the compilation error.


FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;


a) plpgsql doesn't support complex expressions on left side of assign command, b) '' is not NULL in PostgreSQL

you can write your code some like

DECLARE r RECORD;
BEGIN
  FOR i IN array_lower(x, 1) .. array_upper(x, 1)
  LOOP
    r := x[i];
    IF r.reference_value = 'ABC' THEN
      r.reference_value := NULL;
      x[i] := r;
    END IF;
  END LOOP;
END;

Regards

Pavel


 
--
Thanks & Regards,
Brahmeswara Rao J.

Re: How to set array element to null value

От
Pavel Stehule
Дата:


2018-07-09 11:58 GMT+02:00 Brahmam Eswar <brahmam1234@gmail.com>:
I'm trying to reset array element to null. but 3rd line of below snippet is giving the compilation error.


FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
X[indx_1].REFERENCE_VALUE:='';
END IF;
END LOOP;


a) plpgsql doesn't support complex expressions on left side of assign command, b) '' is not NULL in PostgreSQL

you can write your code some like

DECLARE r RECORD;
BEGIN
  FOR i IN array_lower(x, 1) .. array_upper(x, 1)
  LOOP
    r := x[i];
    IF r.reference_value = 'ABC' THEN
      r.reference_value := NULL;
      x[i] := r;
    END IF;
  END LOOP;
END;

Regards

Pavel


 
--
Thanks & Regards,
Brahmeswara Rao J.

Re: How to set array element to null value

От
Thomas Kellerer
Дата:
Brahmam Eswar schrieb am 09.07.2018 um 11:58:
> I'm trying to reset array element to null. but 3rd line of below snippet is giving the compilation error.
> 
> 
> FOR indx_1 IN array_lower(X, 1)..array_upper(X, 1) LOOP
> IF X[indx_1].REFERENCE_VALUE = 'ABC' THEN
> X[indx_1].REFERENCE_VALUE:='';
> END IF;
> END LOOP;

What data type is X exactly? It looks like a composite record type. 

(Also: an empty string '' is not the same as NULL)



Re: How to set array element to null value

От
David Fetter
Дата:
On Mon, Jul 09, 2018 at 03:28:45PM +0530, Brahmam Eswar wrote:
> I'm trying to reset array element to null.

You can do this in SQL as follows:

SELECT ARRAY(
    SELECT CASE e WHEN 'ABC' THEN NULL ELSE e
    FROM UNNEST(x) _(e)
)

This should really be going to pgsql-general because to is about how
to use PostgreSQL, not how to change PostgreSQL.

Best,
David.
-- 
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


Re: How to set array element to null value

От
David Fetter
Дата:
On Mon, Jul 09, 2018 at 03:28:45PM +0530, Brahmam Eswar wrote:
> I'm trying to reset array element to null.

You can do this in SQL as follows:

SELECT ARRAY(
    SELECT CASE e WHEN 'ABC' THEN NULL ELSE e
    FROM UNNEST(x) _(e)
)

This should really be going to pgsql-general because to is about how
to use PostgreSQL, not how to change PostgreSQL.

Best,
David.
-- 
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate