Обсуждение: Combining scalar and row types in RETURNING

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

Combining scalar and row types in RETURNING

От
Ray O'Donnell
Дата:
Can you combine scalar and row types in a RETURNING clause?

My use-case is getting the results of a MERGE - I'd like to be able to 
capture both the action performed and the modified row, something like 
this (this is in a plpgsql function):

declare
     m_action text;
     m_new_data record;
begin
     merge into my_table t
     using (
         ....
     ) s
     on (t.id = s.id)
     when matched then
         update .....
     when not matched then
         insert .....
     returning
         merge_action(), t.*
     into
        m_action, m_new_data;

end;

In my case, m_new_data is actually a table row type rather than plain 
"record". The row is passed on to another function which calculates the 
altered columns and logs the changes.

I've tried it, and it doesn't seem to work; I get an error, "m_new_data 
is not a scalar variable", so I'm guessing it's not possible, but it's 
worth asking... I know I can list the returned columns individually in 
the RETURNING and then use a row constructor to construct the row.... 
but it'd be handier if I could just derive the row directly from the 
MERGE query.

Thanks in advance,

Ray.

-- 
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie