looping multi-dimensional array

Поиск
Список
Период
Сортировка
От Peter Schonefeld
Тема looping multi-dimensional array
Дата
Msg-id 9cc0d1180704031836q3d6c0938v1abd5903cbdd13f6@mail.gmail.com
обсуждение исходный текст
Список pgsql-novice
Hi All,

I'm trying to add a batch of records to a table in one go rather than make a call from the application for each record. To do this i'd like to pass in an array as a text eg. the param looks like:

 { {'bob','myns'} , {'sally','anotherns'} }

Calling the below function doesn't add any row to the empty "user" table and I'm not getting any errors. Can anyone see what i'm doing wrong?

TIA
Pete


CREATE OR REPLACE FUNCTION user_batch_update(text)

DECLARE
  properties text[][] := $1;
  iloop integer = 1;
  myid integer;
BEGIN

WHILE properties[iloop] IS NOT NULL LOOP

    SELECT id INTO myid FROM user WHERE role = properties[iloop][1] AND ns = properties[iloop][2];
    IF NOT FOUND THEN
       INSERT INTO user (role,ns) VALUES (properties[iloop][1],propertie
s[iloop][2]);
    END IF;

    iloop := iloop + 1;
 
  END LOOP;

  RETURN 0;
END;

В списке pgsql-novice по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: What am I doing wrong with this comma-delimited copy?
Следующее
От: Mark Kelly
Дата:
Сообщение: Design advice needed.